我正在将我的游戏移动到自己的包中,以使其在未来的游戏中有用。 我按照其中一位用户的建议实现了一个Builders类:Interfaces, static classes problem
public final class Builders {
public static Builder<? extends Sprite> newGameObjectBuilder(Point location, int drawable) {
return new GameObjectImpl.GameObjectBuilder(location, drawable);
}
}
现在的问题是客户端代码无法使用构建器方法(下面的示例)。我得到以下内容:
"The method onReceiveKey(new Sprite.ReceiveKeys(){}) is undefined for the type Builder<capture#4-of ? extends Sprite>"
基本上我不能使用公共静态调用GameObjectBuilder中的任何方法。
onReceiveKey
无效。只有build()
接口的Builder
方法可用。
public static Sprite newSoldier(int x, int y) {
return Builders.newGameObjectBuilder(new Point(x,y), R.drawable.soldier)
.onReceiveKey(new Sprite.ReceiveKeys() {
@Override
public void onKeyUp(int keyCode, Sprite self, Room room ) {
}
@Override
public void onKeyDown(int keyCode, Sprite self, Room room ) {
if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
self.moveY(room, 1, Direction.S);
}
else if(keyCode == KeyEvent.KEYCODE_DPAD_UP){
self.moveY(room, -1, Direction.N);
}
else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
self.moveX(room, -1,Direction.W);
}
else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
self.moveX(room, 1, Direction.E);
}
}
})
.build();
}
答案 0 :(得分:1)
事情是你的建设者是这样的
public interface Builder<T> {
public T build();
}
只做
public interface SpriteBuilder<T extends Sprite> extends Builder<T> {
Builder<T> onReceiveKey(Sprite.ReceiveKeys receiver);
}
并将Builders更改为
public final class Builders {
public static SpriteBuilder newGameObjectBuilder(Point location, int drawable) {
return new GameObjectImpl.GameObjectBuilder(location, drawable);
}
}
和您的GameObjectBuilder
到
class GameObjectImpl extends AbstractSprite {
public static class GameObjectBuilder implements SpriteBuilder {
...
}
}