English 中文(简体)
Java的一般战略模式
原标题:Strategy pattern with generics in Java

因此,我试图建立一个具有一般功能的界面,由一个子类取代,但我找不到这样做的途径。

我认为,一部法典将更加明确:

interface Agent<A extends Agent<A>> {
    <W extends World> Iterable<Action<A,W>> percepts(W world);
    <W extends World> void takeAction(Action<A,W> action, W world);
}

abstract class StrategistAgent<A extends StrategistAgent<A>> implements Agent<A> {
    abstract public <W extends World> AgentStrategy<A,W> getStrategy();
    abstract public <W extends World> void setStrategy(AgentStrategy<A,W> strategy);

    @Override
    <W extends World> Iterable<Action<A,W>> percepts(W world) {
        return getStrategy().run(this, world);
    }
}

interface AgentStrategy<A extends StrategistAgent<A>, W extends World> {
    Iterable<Action<A,W>> run(A agent, W world);
    void init(A agent, W world);
}

错误: 理论法(A、世界)不适用于论点(战略代理人,W)。

但我不能写:

StrategistAgent<A extends StrategistAgent<A>, W extends World>

http://www.un.org。 Ok I遵循Mark Peters的建议,删除了固定战略方法。 现在应该工作。

最佳回答

因此,正如你现在可能已经指出的,问题是,没有什么东西可以说,如果出现你的错误,,<>this/code>是A。 这就是我在说,这种自我参照的打字确实是常识。

不过,你在你的设计中也遇到其他问题。 对于开端人来说,在你确定战略时,你接受的“可能与“完全不同”。 W,请上percept/code>。

因此,你前进的道路是什么? 我再一次认为,这取决于你的精彩法典,而这种 st子制造了代理人和战略,把他们聚集在一起。

由于这部冰川法很可能确切知道它所处理的哪类制剂和战略,因此你可能不会从制作<条码>时获得惠益>/em>。 一旦不是抽象的,你就只能在每个次级别处理。

Really all you need from your subclasses is a protected method getStrategy. And if you re forcing all your subclasses to provide that, you might as well force them to return the A as well:

abstract class StrategistAgent<A extends StrategistAgent<A>> implements Agent<A> {
    protected abstract <W extends World> AgentStrategy<A,W> getStrategy();
    protected abstract A getSelf();

    @Override
    public <W extends World> Iterable<Action<A,W>> percepts(W world) {
        return this.<W>getStrategy().run(getSelf(), world);
    }
}

因此,这是一个解决办法。 我不得不想一想一个比较棘手的问题。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...