2016-04-04 28 views
0

Java API'ları için temel yanıt sınıfı tasarlama gereksinimim var. Yanıt sınıfı 2 alan içerir - bir durum (String) ve bir hata listesi (List). Ayrıca, ben hata bir temel sınıf olmalıdır aşağıdaki , bunu yapmak için bu temel hata sınıfı (BaseError)Hata ile java API yanıtı

uzanan kendi hata sınıfı var oluşturmak zorunda olacak spesifik kullanım örnekleri olacaktır yapmanız gereken Aşağıdaki sınıfı oluşturdum. Bunu yapmanın en iyi yolu bu mu? Genel bir T'yi kullanmak zorunda kaldım çünkü bir alt tipin listesini temel hata türünün bir listesinin yerine koyamıyorum.

Lütfen bildiriniz.

public class BaseResponse <T extends BaseError> { 

    protected String status; 
    protected List<T> errorList = new ArrayList<T>(); 

     public List<T> getErrorList() { 
      return errorList; 
     } 

     public void setErrorList(List<T> errorList) { 
      this.errorList = errorList; 
     } 

     public String getStatus() { 
      return status; 
     } 

     public void addError(T error) { 
      this.errorList.add(error); 
     } 


} 
+0

Yest bu iyi olduğunu düşünüyorum. Ancak durumu ayarlamak için kurucu eklemelisiniz, – HungPV

+0

durum için ayarlanmış bir yöntem göremiyorum Cevabınız için teşekkür ederiz. –

cevap

0

Eğer varsayılan kurucu ile parametrik kurucusu tanımlamak için ihtiyaç genel sınıf olarak bu sınıfı kullanmak istiyorsanız.

bakınız bu kodu: -

public class BaseResponse <T extends BaseError> { 

protected String status; 
protected List<T> errorList = new ArrayList<T>(); 

public BaseResponse(final String status) { 
    this(status,"200"); 
} 

public BaseResponse(final String status, final List<T> errorList) { 
    super(); 
    this.status = status; 
    this.errorList = errorList; 
} 

    public List<T> getErrorList() { 
     return errorList; 
    } 

    public void setErrorList(List<T> errorList) { 
     this.errorList = errorList; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public void addError(T error) { 
     this.errorList.add(error); 
    } 

}

+0

Yanıt için teşekkür ederiz. –