[英]Error with abstract class and methods
I have an abstract class Player and i want to create two kind of players. 我有一个抽象类的播放器,我想创建两种播放器。 Fast and Technical Players.So i created this abtsract class:
快速和技术玩家。因此,我创建了此abtsract类:
public abstract class Player
{
private int speed;
private int technical;
private Cell playerCell;
public abstract void computeAbility();
public abstract void clonePlayer();
public Player(){
Random rand = new Random();
speed = rand.nextInt(50);
technical = rand.nextInt(50);
}
//i also have accesor and mutator methods
}
public class FastPlayer extends Player
{
private int overallAtrributes;
public FastPlayer(){
super();
setSpeed(2*getSpeed());
}
public void computeOverallAtrributes(){
overallAtrributes = getSpeed() + getTechnical();
}
public void clonePlayer(){
FastPlayer cloneFastPlayer = new FastPlayer();
cloneFastPlayer.setTechnical(getTechnical());
cloneFastPlayer.setSpeed(getSpeed());
}
}
public class TechnicalPlayer extends Player
{
private int overallAtrributes;
public TechnicalPlayer(){
setTechnical(2*getTechnical());
}
public void computeOverallAtrributes(){
overallAtrributes = getSpeed() + getTechnical();
}
public void clonePlayer(){
TechnicalPlayer cloneTechnicalPlayer = new TechnicalPlayer();
cloneTechnicalPlayer.setTechnical(getTechnical());
cloneTechnicalPlayer.setSpeed(getSpeed());
}
}
Now i want create a reproducePlayer method in Player class to produce a Player of the same type as current Player and i want to put the new person in the same Cell. 现在,我想在Player类中创建一个reproducePlayer方法,以产生与当前Player相同类型的Player,并且我想将新人物放置在同一Cell中。 So i created this method in Player class:
所以我在Player类中创建了这个方法:
public void reproduce(){
this.clonePlayer();
//how can i put the new Player in the same Cell with current Player?
}
I also want to create a method movePlayer which moves the Player in a Random neighbor Cell of my array. 我还想创建一个方法movePlayer,将Player移到我数组的随机相邻单元格中。 i dont have create the class Cell yet.
我还没有创建Cell类。
You need to make the clone in the common class.. And than override it or call it. 您需要在公共类中创建克隆。然后重写或调用它。
Something like that: 像这样:
Under player: 在球员下:
public abstract Player clonePlayer();
protected Player clonePlayer(Player newPlayer){
newPlayer.setTechnical(getTechnical());
newPlayer.setSpeed(getSpeed());
return newPlayer;
}
Under the player extended classes: 在播放器扩展类下:
Class TechnicalPlayer extends Player
public Player clonePlayer() {
return super.clonePlayer(new TechnicalPlayer ());
}
This should be something like that.. Hope that helps } 应该是这样的。希望对您有所帮助
Because all your implementing classes have no-args constructors, you could define just one method on the abstract class that uses reflection to create another instance of the same class as the current one: 因为所有实现类都具有无参数构造函数,所以您可以在抽象类上仅定义一个方法,该方法使用反射来创建与当前类相同的另一个实例:
public Player reproducePlayer() {
Player p;
try {
p = getClass().newInstance();
catch (InstantiationException | IllegalAccessException ignore) {}
p.speed = speed;
p.technical = technical;
p.playerCell = playerCell;
return p;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.