我将我的游戏移至其自己的程序包中,以使其在将来的游戏中有用。
我根据一个用户的建议在此处实现了一个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();
}
最佳答案
事情是你的建设者就是这样
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 {
...
}
}
关于java - 将构建器类暴露给客户端代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2378132/