如何用Java
问题描述:
更改子类属性的默认值,这是我的主类,我想改变的如何用Java
其余属性的默认值默认值应为如下:
- 马力:defaultHP
- IP:defaultIP
- traitCooldown:0
- 库存:空
- 法术:空
具有一定价值
package harrypotter.model.character;
import java.awt.Point;
import java.util.ArrayList;
import harrypotter.model.magic.Collectible;
import harrypotter.model.magic.Spell;
public abstract class Wizard {
private String name;
private int defaultHp;
private int defaultIp;
private int hp=defaultHp ;
private int ip=defaultIp;
private ArrayList<Spell> spells;
private ArrayList<Collectible> inventory;
private Point location;
private int traitCooldown;
public Wizard(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDefaultHp() {
return defaultHp;
}
public void setDefaultHp(int defaultHp) {
this.defaultHp = defaultHp;
}
public int getDefaultIp() {
return defaultIp;
}
public void setDefaultIp(int defaultIp) {
this.defaultIp = defaultIp;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getIp() {
return ip;
}
public void setIp(int ip) {
this.ip = ip;
}
public Point getLocation() {
return location;
}
public void setLocation(Point location) {
this.location = location;
}
public int getTraitCooldown() {
return traitCooldown;
}
public void setTraitCooldown(int traitCooldown) {
this.traitCooldown = traitCooldown;
}
public ArrayList<Spell> getSpells() {
return spells;
}
public ArrayList<Collectible> getInventory() {
return inventory;
}
}
子类与我尝试
package harrypotter.model.character;
public class GryffindorWizard extends Wizard implements Champion {
public GryffindorWizard(String name) {
super(name);
this.setHp(900);
this.setIp(500);
this.setTraitCooldown(0);
this.getInventory();
this.getSpells();
}
public void useTrait() {
// TODO Auto-generated method stub
}
}
答
滑稽。你想通去:
this.setHp(900);
this.setIp(500);
那么,为什么不出去:
this.setDefaultHp(900);
this.setDefaultIp(500);
并记录在案;这些分配
private int defaultHp;
private int defaultIp;
private int hp=defaultHp ;
private int ip=defaultIp;
被执行一次当创建一个新对象;因此所有字段在创建时将具有值。换句话说:当你想初始化hp和ip时有一些有意义的默认值;那么考虑在那里使用非0值!
一些代码,但没有真正的问题? – GhostCat
我不知道如何更改每个向导的默认值,所以我如何设置子类中的defaultHp和defaultIp? –
提示:你希望别人花时间帮助你解决问题。那么提供反馈会很礼貌;像“是的,这有助于”(例如通过提升/接受);或者给出一些评论。无论如何:如果你考虑接受我的答案 - 可以等到明天;因为我今天已经达到了每日上限。 – GhostCat