java学习2-运用类成员函数实现对其他子类的操作

运用类成员函数实现对其他子类的操作

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值