如何返回子类类型中的静态实例

时间:2016-02-25 13:47:58

标签: java inheritance static

我目前正在上课。使用简单属性“responseCode”进行响应。 Response有一个静态实例(当它是KO时是一个简单的响应)。

我还有一个类DetailResponse,它是我的类Response的子类。

我要做的是能够使用Response类中的静态变量KO_RESPONSE返回DetailResponse类型的对象。有没有办法简单地做到这一点?

我的超级回应:

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;
    }
}

我的子类DetailResponse扩展了Response:

public class DetailResponse extends Response {
    public String otherField;
}

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;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您试图覆盖static变量,Java不允许这样做。相反,您可以实现以下模式来获取您的基类和子类的实例。这是一个例子:

    public class Response {
     public static Response getInstance(){
      return new Response(<args>);
     }
    }

    public class DetailedResponse extends Response {
     public static DetailedResponse getInstance(){
      return new DetailedResponse(<args>);
     }
    }

最后,Business Class可以实现如下:

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;
        }
    }
}

希望这有帮助!

答案 1 :(得分:-1)

不继承静态变量和方法,但可以使用静态块初始化程序

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;
    }

答案 2 :(得分:-1)

为什么你在这里使用A(),A没有定义。

public static final Response KO_RESPONSE = new A(ReturnCode.KO);

此处可以使用详细的回复或其他类型的回复

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;
    }
}

此外,您必须为可用于定义的详细响应类声明参数化构造函数