枚举抽象问题
我目前正在努力解决 java 抽象问题。我有这样的东西:
public interface State {
};
public interface Dynamics {
getObservationChance(State state, Observation observation);
};
class SpecialState implements State {
};
enum SpecialObservation() {
FREE, WALL, etc.
}
class SpecialDynamics implements Dynamics {
getObservationChance(State state, Observation observation) {
// state should be SpecialState, observation should be SpecialObservation!
}
};
class Main {
Main(State state, Observation observation, Dynamics dynamics) {
dynamics.getObservationChance(state, observation);
}
};
SecialObservation 应该是可能观察的枚举(或类似的东西),但我想要问题的抽象表示。所以我想要一个观察,它应该包含观察和一个返回所有可能观察列表的函数。最后一件事对于我正在实现的算法非常重要,因为我必须总结所有可能的观察结果。
谢谢!
I am currently struggling with a java abstraction problem. I have something like this:
public interface State {
};
public interface Dynamics {
getObservationChance(State state, Observation observation);
};
class SpecialState implements State {
};
enum SpecialObservation() {
FREE, WALL, etc.
}
class SpecialDynamics implements Dynamics {
getObservationChance(State state, Observation observation) {
// state should be SpecialState, observation should be SpecialObservation!
}
};
class Main {
Main(State state, Observation observation, Dynamics dynamics) {
dynamics.getObservationChance(state, observation);
}
};
SecialObservation should be an enum (or something like that) of possible observations, but I want to have an abstract representation of the problem. So I want an Observation that should contain the observation and a function that returns a list of all possible observations. The last thing is very important for an algorithm I am implementing, since I have to summarize over all possible observations.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里需要参数化类型 - 每个树类型都有族:状态、观察和动态。
如果我们将观察枚举作为参数类型,我们可以将您的类型转换为如下所示:
当然,只有当我们的 State 接口的方法足以满足 getObservationChance 方法的情况下,这种方法才有效。
更通用的方法是对所有三种类型进行参数化:
然后我们可以将实现定义如下:
当然,主类需要所有三个参数:
在您的情况下,实际上动态仅取决于观察和状态,而不是相反(并且这些不相互依赖),所以另一种方法是这样的:
编辑:
关于 getAllObservations 方法:
只要您能够以某种方式使类型参数具体化,这里就没有真正的问题。
要访问特定类型的枚举常量列表,您需要直接访问该类型(
SpecialObservation.values()
),或者使用如下所示的类对象:(这只适用于 O当然,它是一个枚举类。)
如果您有一个混合列表,它会变得更加复杂,并且类型安全地匹配 Dynamics、Action、Observation 和 State 类也不是那么容易。
You need parametrized types here - you have families of tree types each: a state, a observation, and a dynamics.
If we take the observation enum as the parameter type, we could convert your type to something like this:
This approach only works if the methods of our State interface are enough for the getObservationChance method, of course.
A more general approach would be to parametrize over all three types:
Then we can define the implementations as this:
The main class then needs all three parameters, of course:
In your case in fact the dynamics is only dependent on the observation and state, and not the other way around (and these are not dependent on each other), so another way would be this:
Edit:
About the getAllObservations method:
As long as you somehow can make your type parameters concrete, there is no real problem here.
To access the list of enum constants of a specific type, you need access to this type - either directly (
SpecialObservation.values()
), or with a class object like here:(This only works if O is an enum class, of course.)
If you have a mixed list, it gets more complicated, and then it is also not really easy to type-safely match the Dynamics, Action, Observation and State classes.
您可以向枚举添加方法:
You could add methods to your enums:
使用接口:
Use an interface:
由于 Java 实现枚举的方式,不可能有抽象枚举,并且通过正确的设计,您将不再需要抽象枚举。然而,据我记得,你可以在枚举中定义你自己的方法......(你可能需要检查一下......我已经有一段时间没有接触Java了)
Because of the way Java implements enums, it is not possible to have abstract enums, and with correct design you will not feeel the need for one. However, so far as I remember, you can define your own methods within enum...(you may have to check this..I've not been in touch with Java for some time now)