简体   繁体   English

从子类中定义的枚举中调用方法-Java

[英]invoke methods from enum defined in subclass - Java

I have an abstract class Sensor, which is extended by multiple Device sub-classes. 我有一个抽象类Sensor,该类被多个Device子类扩展。 Each sub-class defines an enum. 每个子类都定义一个枚举。 These enums can be different for each Sub-class instance. 每个子类实例的这些枚举可以不同。 These enums are used to define a status instance variable. 这些枚举用于定义status实例变量。 I would like to find a way to define the getter method getStatus() in the parent (abstract) class so that I won't have to reapeat the same lines of code in every subclass.. 我想找到一种在父(抽象)类中定义getter方法getStatus()方法,这样我就不必在每个子类中重复相同的代码行。

Eg: 例如:

abstract class Sensor {
    // here I would have liked to declare a "status" variable to hold enum values from subclass
    ....... status;

    Sensor() {}

    public String getStatus() {
        // return the status defined in the subclass
        return status.name();
    }
}
class Device extends Sensor {
    enum Status { ON, OFF };

    Device() {
        super.status = Status.ON;  // store enum value into variable defined in superclass
    }
}
class Device2 extends Sensor {
    enum Status { OPEN, CLOSED, LOCKED };

    Device2() {
        super.status = Status.OPEN;
    }
}

You could make Sensor generic 您可以使Sensor通用

class Sensor<T extends Enum<T>> {
    T status;
    protected Sensor(T status) {
        this.status = status;
    }

    public String getStatus() {
        return this.status.toString();
    }
}

class Device extends Sensor<Device.Status> {
    enum Status {On, Off}
    public Device() {
        super(Status.On);
    }
}

class Device2 extends Sensor<Device2.Status> {
    enum Status { OPEN, CLOSED, LOCKED }

    Device2() {
        super(Status.OPEN);
    }
}

Or you could make Sensor and getStatus() abstract, and not have as a field status in Sensor. 或者,您可以使Sensor和getStatus()抽象,而不在Sensor中将其作为字段状态。

abstract class Sensor {
    public abstract String getStatus();
}

class Device extends Sensor {
    enum Status {On, Off}
    Status status = Status.On;

    public String getStatus() {
        return this.status.toString();
    }
}

class Device2 extends Sensor {
    enum Status { OPEN, CLOSED, LOCKED }
    Status status = Status.OPEN;
    public String getStatus() {
        return this.status.toString();
    }
}

Since you can't inherit from enums in Java and you want the enums to be specific to each sub-class, I would make the superClass generic with the enum 由于您不能从Java中的枚举继承,并且希望枚举特定于每个子类,因此我将使superClass与枚举通用

RQ: I added the static keywords to be use inner classes in a single file. RQ:我添加了static关键字以在单个文件中使用内部类。 You can remove them once you copy them in separate classes/files: 将它们复制到单独的类/文件中后,可以将其删除:

static abstract class Sensor<T extends Enum<T>> {
    T status;

    Sensor() {}

    public T getStatus() {
        return status;
    }

    public String getStatusAsString() {
        return status.name();
    }
}
static class Device extends Sensor<Device.Status> {
    enum Status { ON, OFF };

    Device() {
        super.status = Status.ON;  // store enum value into variable defined in superclass
    }
}
static class Device2 extends Sensor<Device2.Status> {
    enum Status { OPEN, CLOSED, LOCKED };

    Device2() {
        super.status = Status.OPEN;
    }
}

IMO the enum should be part of base class and its value can be modified by a device. IMO枚举应该是基类的一部分,并且其值可以由设备修改。

abstract class Sensor {
    Sensor() {}

    enum Status { ON, OFF };

    Status status;

    public String getStatus() {
        // return the status defined in the subclass
        return status.name();
    }
}

Now consider a Device as: 现在将设备视为:

public class Device extends Sensor {
    Device() {
        super.status = Status.ON;  // store enum value into variable defined in superclass
    }

    public void setStausToOff() {
        super.status = Status.OFF;
    }
}

Then we can do the testing as: 然后我们可以按照以下方式进行测试:

 public static void main(String[] args) {
        Sensor sensor1 = new Device();
        System.out.println(sensor1.getStatus());

        Device sensor2 = new Device();
        System.out.println(sensor1.getStatus());
        sensor2.setStausToOff();
        System.out.println(sensor2.getStatus());

    }

And it prints: 它打印:

ON ON OFF

NOTE: You may need to modify it as per your requirement but it will give you some hint. 注意:您可能需要根据需要进行修改,但这会给您一些提示。

If each Device is going to have its own Enum (distict) in that case its value will be known to the instance of that class only and not to base class. 如果在这种情况下,每个设备都具有自己的Enum(距离),则其值将仅对于该类的实例而不是对基类是已知的。 For example Car is base class and VW is derived class using some special kind features exclusive to this. 例如, Car是基类,而VW是派生类,它使用一些专有的特殊功能。 IN that case you cannot get value of those features in Car class. 在这种情况下,您将无法获得Car类中这些功能的价值。 How can parent class know about the attribute specific to a child class? 父类如何知道子类的特定属性? It cannot. 这不可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM