[英]How to return a static instance in subclass type
I am currently two class. 我目前是两个班。 Response with a simple attribute "responseCode". 带有简单属性“ responseCode”的响应。 Response has a static instance (A simple response when it is KO). 响应具有静态实例(为KO时为简单响应)。
I have also a class DetailResponse which is a subclass of my class Response. 我还有一个类DetailResponse,它是我的类Response的子类。
What I would do is to be able to return an object of type DetailResponse using my static variable KO_RESPONSE in Response class. 我要做的是能够使用Response类中的静态变量KO_RESPONSE返回类型DetailResponse的对象。 Is there a way to do it simply ? 有没有办法做到这一点?
My super class Response : 我的超级班级回应:
public class Response {
public static final Response KO_RESPONSE = new A(ReturnCode.KO);
public ReturnCode responseCode;
public String comment;
public Response () {};
public Response (ReturnCode responseCode) {
this.responseCode = responseCode;
}
public static enum ReturnCode {
OK,
KO;
}
}
My subclass DetailResponse which extends Response : 我的子类DetailResponse扩展了Response:
public class DetailResponse extends Response {
public String otherField;
}
BuisinessService class : BuisinessService类别:
public BuisinessService {
public DetailResponse sendRequest() {
String status = sendRequest() // Do something
if(status.equals("KO")) {
return Response.KO_RESPONSE; // What I would do but doesn't work because Response is the super class of DetailResponse
} else {
DetailResponse detail = new DetailResponse(ReturnCode.OK);
detail.comment = "comment";
detail.otherField = "somethingCool";
return detail;
}
}
}
You are trying to override static
variables, Java doesn't allow this. 您正在尝试覆盖static
变量,Java不允许这样做。 Instead you can implement below pattern to get instances of your base and child class. 相反,您可以实现以下模式来获取基类和子类的实例。 Here is an example: 这是一个例子:
public class Response {
public static Response getInstance(){
return new Response(<args>);
}
}
public class DetailedResponse extends Response {
public static DetailedResponse getInstance(){
return new DetailedResponse(<args>);
}
}
Finally the Business Class can be implemented as below: 最后,可以按以下方式实现商务舱:
public BuisinessService {
public DetailResponse sendRequest() {
String status = sendRequest() // Do something
if(status.equals("KO")) {
return DetailResponse.getInstance(); // What I would do but doesn't work because Response is the super class of DetailResponse
} else {
DetailResponse detail = new DetailResponse(ReturnCode.OK);
detail.comment = "comment";
detail.otherField = "somethingCool";
return detail;
}
}
}
Hope this helps! 希望这可以帮助!
Static variables and method are not inherited, but you can use a static block initializer 静态变量和方法不会继承,但是您可以使用静态块初始化程序
public class Response {
public static Response KO_RESPONSE;
public ReturnCode responseCode;
public String comment;
public Response () {};
public Response (ReturnCode responseCode) {
this.responseCode = responseCode;
}
public static enum ReturnCode {
OK,
KO;
}
}
public class DetailResponse extends Response {
static {
Response.KO_RESPONSE = new DetailResponse(/* .. */);
}
public String otherField;
}
Why you have used A() here, A is not defined. 为什么在这里使用A(),但未定义A。
public static final Response KO_RESPONSE = new A(ReturnCode.KO);
Detailed response or other type of response can be used here 可以在此处使用详细响应或其他类型的响应
class DetailResponse extends Response {
public DetailResponse(ReturnCode ko) {
super(ko);
}
public String otherField;
}
class Response {
public static final Response KO_RESPONSE = new Response(ReturnCode.KO);
public ReturnCode responseCode;
public String comment;
public Response () {};
public Response (ReturnCode responseCode) {
this.responseCode = responseCode;
}
public static enum ReturnCode {
OK,
KO;
}
}
Also you must have to declare a parameterized constructor for your detailed response class that can be used to defined 此外,您还必须为详细的响应类声明一个参数化的构造函数,该类可用于定义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.