简体   繁体   English

如何以子类类型返回静态实例

[英]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.

相关问题 如何返回参数化返回类型的子类,其参数类型是返回类型的参数类型的子类? - How to return a subclass of parameterized return type whose parametric type is a subclass of the return type's parametric type? Java:如何使用超类的静态方法从其创建子类的实例 - Java: How to Create an instance of a subclass from with a Static method of the super class 通过子类实例调用时,如何在子类的方法中使用子类的静态字段? - How do I use a subclass' static fields in superclass' method when calling via instance of subclass? 如何在子类的实例方法中返回超类对象? - How to return an superclass object in a subclass's instance method? 在另一个函数中创建子类的实例时,为什么不能从子类类型的 Abstact 类调用方法(公共或静态)? - Why can't I call a method (public or static) from an Abstact class of type subclass when creating an instance of the subclass in another function? 使用超类类型作为子类实例 - Using superclass type for subclass instance 使用超类静态方法获取子类的实例 - Get instance of subclass with superclass static method 在Java中,如何使子类的实例变量具有子类的类型,而不是父类的类型? - In Java, how do I make instance variables of a subclass have the type of the subclass, rather than of the superclass? 如果子类中的实例方法具有相同的签名和DIFFERENT返回类型,那么它是重写方法还是新方法? - If an instance method in the subclass has the same signature and a DIFFERENT return type, is it a overriding method or a new method? 实例化T的子类时如何返回泛型T - How to return generic type T when instantiating a subclass of T
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM