我正在尝试使用TUI制作小型游戏。TUI应该具有一个get()
方法,该方法应使用String question
和返回类型作为参数,因此您可以使用它来请求对象。
例如,get("What is your name?", String)
应返回String
,get("What color do you want?", Color)
应返回Color
。
这是我想出的:
public interface View {
public <T> T get(String question, Class<T> type) throws InputException;
}
public class TUI implements View {
public Color get(String question, Class<Color> type) {
// code
}
public String get(String question, Class<String> type) {
// code
}
}
Color color = Game.getView().get("What piece do you want to place?", Color.class);
不幸的是,中的方法TUI
不被接受为public的实现<T> T get(String question, Class<T> type) throws InputException;
。有什么办法可以做到吗?
通过拆分功能来保持类型安全似乎更合适:
Color color = Game.getView().getColor("What piece do you want to place?");
String string = Game.getView().getString("What piece do you want to place?");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句