示例
public class ADHero extends Hero {
public void attack() {
System.out.println(name + " 进行了一次攻击 ,但是不确定打中谁了");
}
// 可变数量的参数
public void attack(Hero... heros) {
for (int i = 0; i < heros.length; i++) {
System.out.println(name + " 攻击了 " + heros[i].name);
}
}
public static void main(String[] args) {
ADHero bh = new ADHero();
bh.name = "赏金猎人";
Hero h1 = new Hero();
h1.name = "盖伦";
Hero h2 = new Hero();
h2.name = "提莫";
bh.attack(h1);
bh.attack(h1, h2);
}
}
设计一个类Support (辅助英雄)继承Hero,提供一个heal(治疗)方法
对Support的heal方法进行重载
heal()
heal(Hero h) //为指定的英雄加血
heal(Hero h, int hp) //为指定的英雄加了hp的血
public class Support extends Hero {
public void heal(){
System.out.println("负责为自己加血");
}
public void heal(Hero h){
System.out.println("负责为"+h.name+"加血");
}
public void heal(Hero h,int hp){
System.out.println("负责为"+h.name+"加了"+hp+"的血");
}
public static void main(String[] args) {
Support s=new Support();
s.name="猎人";
s.heal();
s.heal(s);
s.heal(s,20);
}
答案解析