Java-枚举通配符

发布于 2024-10-26 06:03:29 字数 1024 浏览 3 评论 0原文

我在实现 Java 功能时遇到一些问题。

我有一个传感器列表。我有不同种类的它们,它们都扩展了基类Sensor。 我在基类中有一些抽象函数,我希望这些函数采用 Enum 作为参数。问题是每个子类的枚举都是唯一的,因此,我无法在基类中声明枚举。

下面的代码使用 Enum 作为参数。我知道这不是合法的语法,但我只是想说明这是我想要将子类 Enum 作为参数的地方。


private Vector<Sensor> sensors;

public abstract class Sensor {  
    public Sensor() {}  
    public abstract int getParam(Enum param);  
    public abstract void setParam(Enum param, int value);  
}

public class TempSensor extends Sensor {
    // Parameter names
    public static enum TEMP_PARAMETERS{ PARAM_ALARM_HI, PARAM_ALARM_LO }

    public TempSensor() {}

    @Override
    public int getParam(TEMP_PARAMETERS param) {
            // Will do some stuff here
            return 0;
    }

    @Override
    public void setParam(TEMP_PARAMETERS param, int value) {
            // Will do some stuff here
    }
}

如果不同的枚举实现了一个接口,我可以使用该接口作为抽象方法中的参数类型,但随后我可以将不属于各自类的枚举作为参数传递。有办法避免吗?

I have some problems implementing a Java feature.

I have a list of Sensors. I have different kinds of them, they all extend the base class Sensor.
I have some abstract functions in the base class, and I want these functions to take an Enum as a parameter. The problem is that the Enum is unique for each sub class, and therefore, I can't declare the Enum in the base class.

The code below has Enum as parameter. I know it's not legal syntax, but I just want to illustrate that this is where I want to have the sub class Enum as parameter.


private Vector<Sensor> sensors;

public abstract class Sensor {  
    public Sensor() {}  
    public abstract int getParam(Enum param);  
    public abstract void setParam(Enum param, int value);  
}

public class TempSensor extends Sensor {
    // Parameter names
    public static enum TEMP_PARAMETERS{ PARAM_ALARM_HI, PARAM_ALARM_LO }

    public TempSensor() {}

    @Override
    public int getParam(TEMP_PARAMETERS param) {
            // Will do some stuff here
            return 0;
    }

    @Override
    public void setParam(TEMP_PARAMETERS param, int value) {
            // Will do some stuff here
    }
}

If the different Enums implement an interface, I can use the interface as the parameter type in the abstract methods, but then I can pass Enums that don't belong to the respective class as parameter. Is there a way to avoid that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风来 2024-11-02 06:03:29

你走在正确的轨道上。假设您有一个名为 MyEnumTypeInterface 的标记接口,只需让不同的枚举实现该接口即可。然后使用 MyEnumTypeInterface 作为接受枚举的方法的形式参数类型。但是,您需要确保获得一个实现 MyEnumTypeInterface 的枚举,而不仅仅是实现 MyEnumTypeInterface 的任何其他类:

public <E extends Enum<E> & MyEnumTypeInterface>void getParam(E e)

这可确保形式参数是一个 enum 并且它实现了 MyEnumTypeInterface;该方法不会接受另一个也实现 MyEnumTypeInterface 的类作为参数。

所以你的课程最终看起来像这样:

public interface MyEnumTypeInterface {
}

public abstract class Sensor {  
    public Sensor() {}  
    public abstract <E extends Enum<E> & MyEnumTypeInterface>int getParam(E param);  
    public abstract <E extends Enum<E> & MyEnumTypeInterface>void setParam(E param, int value);  
}

public enum TempEnum extends MyEnumTypeInterface {
    PARAM_ALARM_HI, 
    PARAM_ALARM_LO
}


public class TempSensor extends Sensor {

    public TempSensor() {}

    @Override
    public<E extends Enum<E> & MyEnumTypeInterface>int getParam(E param) {
            return 0;
    }

    @Override
    public <E extends Enum<E> & MyEnumTypeInterface>void setParam(E param, int value) {
            // Will do some stuff here
    }
}

You're on the right track. Assuming you have a marker interface called MyEnumTypeInterface, just have your different enums implement that inferface. Then use MyEnumTypeInterface as the type of the formal parameter for your methods that accept the enum. However, you need to ensure that you're getting an enum that implements MyEnumTypeInterface and not just any other class that implements MyEnumTypeInterface:

public <E extends Enum<E> & MyEnumTypeInterface>void getParam(E e)

This ensures that the formal parameter is an enum and that it implements MyEnumTypeInterface; the methed won't accept as a parameter, another class that also implements MyEnumTypeInterface.

So your classes end up looking like this:

public interface MyEnumTypeInterface {
}

public abstract class Sensor {  
    public Sensor() {}  
    public abstract <E extends Enum<E> & MyEnumTypeInterface>int getParam(E param);  
    public abstract <E extends Enum<E> & MyEnumTypeInterface>void setParam(E param, int value);  
}

public enum TempEnum extends MyEnumTypeInterface {
    PARAM_ALARM_HI, 
    PARAM_ALARM_LO
}


public class TempSensor extends Sensor {

    public TempSensor() {}

    @Override
    public<E extends Enum<E> & MyEnumTypeInterface>int getParam(E param) {
            return 0;
    }

    @Override
    public <E extends Enum<E> & MyEnumTypeInterface>void setParam(E param, int value) {
            // Will do some stuff here
    }
}
森林散布 2024-11-02 06:03:29

看起来你想要矛盾的东西。使用多态性的全部意义在于利用替换原则
如果您想要有一个类层次结构并确保将正确的类型输入到正确的对象中,您可以考虑使用工厂模式。
我强烈建议不要继承枚举; Java 处理得不太好。

Looks like you want contradictory things. The whole point of using polymorphism is to take advantage of the substitution principle.
If you want to have a class hierarchy and be sure the right type is entered to the right object, you may consider using the factory pattern.
I strongly recommend against inheritance on Enums; Java doesn't handle that well.

染年凉城似染瑾 2024-11-02 06:03:29

那么您希望每个 Sensor 工作于特定的参数类型吗?这意味着使 Sensor 变得通用。

public abstract class Sensor<P extend Enum<P>> {  
    public Sensor() {}  
    public abstract int getParam(P param);  
    public abstract void setParam(P param, int value);  
}

您的设计可能存在更大的问题。修复这些问题可以消除对获取和设置的要求。

So you want each Sensor to work a particular param type? That would mean making Sensor generic.

public abstract class Sensor<P extend Enum<P>> {  
    public Sensor() {}  
    public abstract int getParam(P param);  
    public abstract void setParam(P param, int value);  
}

There are probably bigger problems with you design. Fixing those could remove the requirement for the get and set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文