运用类成员函数实现对其他子类的操作
public class Zhaoyun {
String name = "zhaoyun";
int blood =5000;
int atk =500;
//zhaoyun attack houyi
public void ATK(Houyi hy){
hy.blood-=atk;
System.out.println(name+" attack "+hy.name+",the rest of "+hy.name+" is "+hy.blood);
}
}
特点
1.一对一或者一对多,在传入对象为一个类对象的情况下,实现一个类成员函数对一个类对象的操作
Key point
只在一个类中写入程序入口 并在其中编辑主程序
EX:
public class Hedao {
public static void main (String[] args){
Zhaoyun zy=new Zhaoyun();
Houyi hy=new Houyi();
//zhaoyun and houyi attack each other tiil somebody died
while (zy.blood>0 && hy.blood>0){
hy.ATK(zy);
zy.ATK(hy);System.out.println();
}
//figure out who is winner , the data of winner give to hy;
if(zy.blood>0){
System.out.println("the winner is "+zy.name);
hy.blood=zy.blood;
hy.atk=zy.atk;
}
else System.out.println("the winner is "+hy.name);
}
}