第二次尝试this问题(初始代码不足以突出问题)
这是无法编译的代码:
interface Player<R, G extends Game>
{
R takeTurn(G game);
}
interface Game<P extends Player>
{
void play(P player);
}
abstract class AbstractGame<R, P extends Player>
implements Game<P>
{
public final void play(final P player)
{
final R value;
value = player.takeTurn(this);
turnTaken(value);
}
protected abstract void turnTaken(R value);
}
public class XPlayer
implements Player<Integer, XGame>
{
@Override
public Integer takeTurn(final XGame game)
{
return (42);
}
}
public class XGame<P extends Player<Integer, XGame>>
extends AbstractGame<Integer, XPlayer>
{
@Override
protected void turnTaken(final Integer value)
{
System.out.println("value = " + value);
}
}
public class Main
{
public static void main(final String[] argv)
{
final XPlayer player;
final XGame game;
player = new XPlayer();
game = new XGame();
game.play(player);
}
}
我遇到的是试图让AbstractGame中的play方法进行编译。似乎我必须与Game和Player一起在扩展/实现中添加泛型,但我一生都无法理解。
play方法必须在AbstractGame类中是最终方法,并且无法进行强制转换,而且我不想编写另一种方法(如turnTaken)来使它能够正常工作。
编辑:这里要求的是编译的代码,但需要强制转换:
interface Player<R, P extends Player<R, P, G>, G extends Game<R, G, P>>
{
R takeTurn(G game);
}
interface Game<R, G extends Game<R, G, P>, P extends Player<R, P, G>>
{
void play(P player);
}
abstract class AbstractGame<R, G extends Game<R, G, P>, P extends Player<R, P, G>>
implements Game<R, G, P>
{
public final void play(final P player)
{
final R value;
value = player.takeTurn((G)this);
turnTaken(value);
}
protected abstract void turnTaken(R value);
}
class XPlayer
implements Player<Integer, XPlayer, XGame>
{
@Override
public Integer takeTurn(final XGame game)
{
return (42);
}
}
class XGame
extends AbstractGame<Integer, XGame, XPlayer>
{
@Override
protected void turnTaken(final Integer value)
{
System.out.println("value = " + value);
}
}
class Main
{
public static void main(final String[] argv)
{
final XPlayer player;
final XGame game;
player = new XPlayer();
game = new XGame();
game.play(player);
}
}
最佳答案
混合泛型和原始类型是行不通的。如果需要这些接口互相引用,则它们还需要引用自己:
interface Player<R, P extends Player<R, P, G>, G extends Game<R, G, P>>
{
R takeTurn(G game);
}
interface Game<R, G extends Game<R, G, P>, P extends Player<R, P, G>>
{
void play(P player);
}
尽管这看起来很头疼,但我不确定您为什么需要它。
编辑:
我能够基于上述实现您的
AbstractGame
:abstract class AbstractGame<R, P extends Player<R, P, AbstractGame<R, P>>>
implements Game<R, AbstractGame<R, P>, P>
{
public final void play(final P player)
{
final R value;
value = player.takeTurn(this);
turnTaken(value);
}
protected abstract void turnTaken(R value);
}
但是我不能完全使用
XGame
和XPlayer
封闭电路:public class XGame
extends AbstractGame<Integer, XPlayer> //compile error on XPlayer
{
protected void turnTaken(Integer value) { }
}
public class XPlayer
implements Player<Integer, XPlayer, XGame> //compile error on XGame
{
@Override
public Integer takeTurn(final XGame game)
{
return (42);
}
}
问题似乎是
XGame
和XPlayer
的每个通用声明都需要彼此正确。这是您的设计真正具有周期性的地方。如果编译器“假设”每一个都是正确的,那么理论上它将起作用。但事实并非如此。编辑2:
这个怎么样:
interface Game<R, G extends Game<R, G>>
{
void play(Player<R, G> player);
}
interface Player<R, G extends Game<R, G>>
{
R takeTurn(G game);
}
abstract class AbstractGame<R, G extends AbstractGame<R, G>>
implements Game<R, G>
{
public final void play(final Player<R, G> player)
{
final R value;
value = player.takeTurn(self());
turnTaken(value);
}
protected abstract G self();
protected abstract void turnTaken(R value);
}
public final class XGame extends AbstractGame<Integer, XGame>
{
protected XGame self() {
return this;
}
protected void turnTaken(Integer value) { }
}
public class XPlayer implements Player<Integer, XGame>
{
@Override
public Integer takeTurn(final XGame game)
{
return (42);
}
}
此处的关键是在
self()
中声明一个抽象方法AbstractGame
,该方法返回G
类型的实例。扩展类必须使用自己的类型来解析继承的type参数,并实现self()
以返回this
。这仅适用于内部代码,因为扩展类很容易说谎,例如:public class EvilGame extends AbstractGame<Integer, AnotherGame> { ... }
有关此模式的更多详细信息,请参见我的答案here和this post。