使用具有多态性的反射来实例化 class 的最佳方法是什么?
What is the best way to instantiate a class using Reflection with the polymorphism?
I'm trying to use Reflection in java to
instantiate a Player with a Command Pattern likes below :
一条命令中只有一个'execute'方法class,
而InitiatePlayerCommand作为它的名字,它会实例化一个
subclass of Player 根据构造函数中传入的playerClass
I have two kind of subclass of Player class : HumanPlayer and AiPlayer
for polymorphism.
我希望它会实例化其中一个 subclass 并添加到 playerList
但我不知道用反射达到这个目的的最佳方法是什么。它总是发生类型转换错误。
public class InitiatePlayerCommand implements Command{
private List<Player> playerList;
private String name;
private WarMode warMode;
private Class<?> playerClass;
public <T extends Player> InitiatePlayerCommand(String name, WarMode mode ,List<Player> list
,Class<T> playerClass){
this.name = name;
this.warMode = mode;
this.playerList = list;
this.playerClass = playerClass;
}
@Override
public void execute() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
Player player = (Player) playerClass.newInstance();
player.setName(name);
player.setWarMode(warMode);
playerList.add(player);
}
public static void main(String[] argv){
List<Player> playerList = new ArrayList<Player>();
new InitiatePlayerCommand("Johnny",WarMode.MILITARY,playerList,HumanPlayer.class)
.execute(); // this line get an error HumanPlayer.class isn't compatible to Class<T extends Player> playerClass
System.out.println(playerList);
}
}
有什么方法可以不使用 Class>
达到这个
您应该考虑使用 Supplier<T>
,这是一个 returns 对象的无参数函数。
在这种情况下,您的 Supplier
可能定义如下:
Supplier<Player> playerSupplier = () -> new HumanPlayer();
然后您可以像这样使用它:
public void execute() {
Player player = playerSupplier.get();
player.setName(name);
player.setWarMode(warMode);
playerList.add(player);
}
I'm trying to use Reflection in java to
instantiate a Player with a Command Pattern likes below :
一条命令中只有一个'execute'方法class,
而InitiatePlayerCommand作为它的名字,它会实例化一个
subclass of Player 根据构造函数中传入的playerClass
I have two kind of subclass of Player class : HumanPlayer and AiPlayer for polymorphism.
我希望它会实例化其中一个 subclass 并添加到 playerList
但我不知道用反射达到这个目的的最佳方法是什么。它总是发生类型转换错误。
public class InitiatePlayerCommand implements Command{
private List<Player> playerList;
private String name;
private WarMode warMode;
private Class<?> playerClass;
public <T extends Player> InitiatePlayerCommand(String name, WarMode mode ,List<Player> list
,Class<T> playerClass){
this.name = name;
this.warMode = mode;
this.playerList = list;
this.playerClass = playerClass;
}
@Override
public void execute() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
Player player = (Player) playerClass.newInstance();
player.setName(name);
player.setWarMode(warMode);
playerList.add(player);
}
public static void main(String[] argv){
List<Player> playerList = new ArrayList<Player>();
new InitiatePlayerCommand("Johnny",WarMode.MILITARY,playerList,HumanPlayer.class)
.execute(); // this line get an error HumanPlayer.class isn't compatible to Class<T extends Player> playerClass
System.out.println(playerList);
}
}
有什么方法可以不使用 Class>
达到这个您应该考虑使用 Supplier<T>
,这是一个 returns 对象的无参数函数。
在这种情况下,您的 Supplier
可能定义如下:
Supplier<Player> playerSupplier = () -> new HumanPlayer();
然后您可以像这样使用它:
public void execute() {
Player player = playerSupplier.get();
player.setName(name);
player.setWarMode(warMode);
playerList.add(player);
}