1.自动转型应用
- 子类的对象可以自动转型成父类的对象
- 这是一种安全的转换
2.多态
- 多态的概述:同类型的对象,执行同一个行为,会表现出不同的行为特征。
代码实现
- 明确角色的属性和攻击方式(以角色创建类,属性为方法属性,攻击方式为方法)
public class HouYI {
String nameID;
int ad;
int hp;
int lv;
public void init(String id) {
this.nameID = "后羿(" + id + ")";
this.ad = 230;
this.hp = 6600;
this.lv = 2;
}
public void atk(LvBU lb) {
int num = (int) (160 + lv * 40 + 0.5 * ad);
lb.hp -= num;
System.out.println(nameID + "攻击了" + lb.nameID + "使其失去了" +
num + "点HP,其还剩下" + lb.hp);
}
public void atk(ZhangFei zf) {
int num = (int) (160 + lv * 40 + 0.5 * ad);
zf.hp -= num;
System.out.println(nameID + "攻击了" + zf.nameID + "使其失去了" +
num + "点HP,其还剩下" + zf.hp);
}
}
public class LvBU {
String nameID;
int ad;
int hp;
int lv;
public void init(String id) {
this.nameID = "吕布(" + id + ")";
this.ad = 50;
this.hp = 4600;
this.lv = 2;
}
public void atk(HouYI hy) {
int num = (int) (480 + lv * 120 + 2.6 * ad);
hy.hp -= num;
System.out.println(nameID + "攻击了" + hy.nameID + "使其失去了" +
num + "点HP,其还剩下" + hy.hp);
}
public void atk(ZhangFei zf) {
int num = (int) (480 + lv * 120 + 2.6 * ad);
zf.hp -= num;
System.out.println(nameID + "攻击了" + zf.nameID + "使其失去了" +
num + "点HP,其还剩下" + zf.hp);
}
}
- 当同时存在多个角色时,创建角色类的代码长度会快速增长,且基本相同或相似的。故需要类与类的继承简化代码
- 优化角色属性,创建父类Hero类,包含角色属性和ID的属性和构建方法
public class Hero {
String nameID;
int ad;
int hp;
int lv;
public void init(String id) {
this.nameID = "(" + id + ")";
this.ad = 50;
this.hp = 4600;
this.lv = 2;
}
}
- 同一角色类的攻击方法相同,仅对象不同,是方法的重载,可以将参数类型向上转型,这样我们调用攻击方法,它的参数类型除了可以给它本身这种类型之外,实际上只要是它的子类都可以。
public class HouYI extends Hero {
public void init(String id) {
super.init(id);
this.nameID = "后羿" + nameID;
}
public void atk(Hero lb) {
int num = (int) (160 + lv * 40 + 0.5 * ad);
lb.hp -= num;
System.out.println(nameID + "攻击了" + lb.nameID + "使其失去了" +
num + "点HP,其还剩下" + lb.hp);
}
}
public class Hero {
String nameID;
int ad;
int hp;
int lv;
public void init(String id) {
this.nameID = "(" + id + ")";
this.ad = 50;
this.hp = 4600;
this.lv = 2;
}
}
public class HouYI extends Hero {
public void init(String id) {
super.init(id);
this.nameID = "后羿" + nameID;
}
public void atk(Hero lb) {
int num = (int) (160 + lv * 40 + 0.5 * ad);
lb.hp -= num;
System.out.println(nameID + "攻击了" + lb.nameID + "使其失去了" +
num + "点HP,其还剩下" + lb.hp);
}
}
public class LvBU extends Hero {
public void init(String id) {
super.init(id);
this.nameID = "吕布" + nameID;
}
public void atk(Hero hero) {
int num = (int) (480 + lv * 120 + 2.6 * ad);
hero.hp -= num;
System.out.println(nameID + "攻击了" + hero.nameID + "使其失去了" +
num + "点HP,其还剩下" + hero.hp);
}
public class ZhangFei extends Hero {
public void init(String id) {
super.init(id);
this.nameID = "张飞" + nameID;
}
public void atk(Hero hy) {
int num = (350 + lv * 100 + ad);
hy.hp -= num;
System.out.println(nameID + "攻击了" + hy.nameID + "使其失去了" +
num + "点HP,其还剩下" + hy.hp);
}
}
}
public class PKGame {
public static void main(String[] args) {
HouYI hy1 = new HouYI();
LvBU lb1 = new LvBU();
hy1.init("小天");
lb1.init("小嘿");
while (hy1.hp > 0 && lb1.hp > 0) {
hy1.atk(lb1);
Hero hero = hy1;
lb1.atk(hero);
}
ZhangFei zf1 = new ZhangFei();
zf1.init("小红");
if (hy1.hp > 0) {
System.out.println(hy1.nameID + "Win~~");
while (hy1.hp > 0 && zf1.hp > 0) {
hy1.atk(zf1);
zf1.atk(hy1);
}
} else {
System.out.println(lb1.nameID + "Win~~");
while (lb1.hp > 0 && zf1.hp > 0) {
Hero hero = zf1;
lb1.atk(hero);
zf1.atk(lb1);
}
}
}
}