簡體   English   中英

如何以子類類型返回靜態實例

[英]How to return a static instance in subclass type

我目前是兩個班。 帶有簡單屬性“ responseCode”的響應。 響應具有靜態實例(為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;
        }
    }
}

您正在嘗試覆蓋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>);
     }
    }

最后,可以按以下方式實現商務艙:

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

希望這可以幫助!

靜態變量和方法不會繼承,但是您可以使用靜態塊初始化程序

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

為什么在這里使用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;
    }
}

此外,您還必須為詳細的響應類聲明一個參數化的構造函數,該類可用於定義

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM