Java-枚举通配符
我在实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你走在正确的轨道上。假设您有一个名为 MyEnumTypeInterface 的标记接口,只需让不同的枚举实现该接口即可。然后使用 MyEnumTypeInterface 作为接受枚举的方法的形式参数类型。但是,您需要确保获得一个实现
MyEnumTypeInterface
的枚举,而不仅仅是实现MyEnumTypeInterface
的任何其他类:这可确保形式参数是一个
enum
并且它实现了MyEnumTypeInterface
;该方法不会接受另一个也实现 MyEnumTypeInterface 的类作为参数。所以你的课程最终看起来像这样:
You're on the right track. Assuming you have a marker interface called
MyEnumTypeInterface
, just have your different enums implement that inferface. Then useMyEnumTypeInterface
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 implementsMyEnumTypeInterface
and not just any other class that implementsMyEnumTypeInterface
:This ensures that the formal parameter is an
enum
and that it implementsMyEnumTypeInterface
; the methed won't accept as a parameter, another class that also implementsMyEnumTypeInterface
.So your classes end up looking like this:
看起来你想要矛盾的东西。使用多态性的全部意义在于利用替换原则。
如果您想要有一个类层次结构并确保将正确的类型输入到正确的对象中,您可以考虑使用工厂模式。
我强烈建议不要继承枚举; 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.
那么您希望每个
Sensor
工作于特定的参数类型吗?这意味着使Sensor
变得通用。您的设计可能存在更大的问题。修复这些问题可以消除对获取和设置的要求。
So you want each
Sensor
to work a particular param type? That would mean makingSensor
generic.There are probably bigger problems with you design. Fixing those could remove the requirement for the get and set.