java继承、方法重载

设计一个类Support (辅助英雄)继承Hero,提供一个heal(治疗)方法
对Support的heal方法进行重载
heal()
heal(Hero h) //为指定的英雄加血
heal(Hero h, int hp) //为指定的英雄加了hp的血

设计一个类Armor护甲
继承Item类,并且额外提供一个属性ac: 护甲等级 int类型
实例化出两件护甲
名称 价格 护甲等级
布甲 300 15
锁子甲 500 40

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
设计一个类Armor护甲
继承Item类,并且额外提供一个属性ac: 护甲等级 int类型
实例化出两件护甲
名称 价格 护甲等级
布甲 300 15
锁子甲 500 40
*/
class day0916
{
public static void main(String[] args)
{
Armor bujia=new Armor();
Armor suozijia=new Armor();
bujia.name="布甲";
bujia.price=300;
bujia.ac=15;
suozijia.name="锁子甲";
suozijia.price=500;
suozijia.ac=40;
System.out.println("名字:"+bujia.name+" 价格:"+bujia.price+" 护甲等级:"+bujia.ac);
System.out.println("名字:"+suozijia.name+" 价格:"+suozijia.price+" 护甲等级:"+suozijia.ac);
Hero timo=new Hero();
timo.name="提莫";
timo.hp=333.0f;
timo.armor=14.0f;
timo.moveSpeed=330;
timo.outPut();
System.out.println("*****************************");
Hero gailun=new Hero();
gailun.name="盖伦";
gailun.hp=616.0f;
gailun.armor=27.0f;
gailun.moveSpeed=250;
gailun.outPut();
System.out.println("*****************************");
Support naima=new Support();
naima.name="众星之子";
naima.hp=345f;
naima.armor=30f;
naima.moveSpeed=400;
naima.heal(timo);
System.out.println("*****************************");
System.out.println("治疗后");
timo.outPut();
naima.heal(gailun);
System.out.println("*****************************");
System.out.println("治疗后");
gailun.outPut();


}
}
class Item//物品类
{
String name;
int price;
}
class Armor extends Item
{
int ac;//护甲等级
}
/*
设计一个类Support (辅助英雄)继承Hero,提供一个heal(治疗)方法
对Support的heal方法进行重载
heal()
heal(Hero h) //为指定的英雄加血
heal(Hero h, int hp) //为指定的英雄加了hp的血
*/
class Hero
{
String name;
float hp;
float armor;
int moveSpeed;
void outPut()
{
System.out.println("英雄名称:"+name+"\n英雄生命值:"+hp+"\n英雄护甲:"+armor+"\n移动速度:"+moveSpeed);
}
}
class Support extends Hero
{
public static String name;
public static void heal(Hero...heros)
{
for (int i=0;i<heros.length;i++ )//重载多次 解决累赘
{
heros[i].hp+=150;
System.out.println(name+"给"+heros[i].name+"治疗了150生命值");
}
}
}

运行;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值