码迷,mamicode.com
首页 > 其他好文 > 详细

类之间的关系练习2

时间:2017-04-17 17:32:56      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:bow   power   class   使用   setw   system   nbsp   span   ted   

package lianxi2;

 public abstract class Weapon {

    public abstract String getName();
    public abstract int getPower();
    
    
    
}
package lianxi2;

public class Knife extends Weapon {

    @Override
    public String getName() {
        
        return "大刀";
    }

    @Override
    public int getPower() {
        // TODO Auto-generated method stub
        return 10;
    }

}
package lianxi2;

public class Bow extends Weapon {

    public String getName() {
        return "后裔强弓";
    }

    public int getPower() {
        
        return 20;
    }

}
package lianxi2;

public class Hero {
Weapon weapon;
String name;
public Hero(Weapon weapon, String name) {
    super();
    this.weapon = weapon;
    this.name = name;
}

public void attack(Enemy e)    {
    e.hurt(weapon.getPower());
    System.out.println(this.name+"使用"+weapon.getName()+"攻击"+e.name);
    e.showInfo();
}

public Weapon getWeapon() {
    return weapon;
}

public void setWeapon(Weapon weapon) {
    this.weapon = weapon;
}
    
    
}
package lianxi2;

public class Enemy {
    int power=100;
    String name;
    
    public Enemy(int power, String name) {
        super();
        this.power = power;
        this.name = name;
    }
    public void hurt(int blood){
        this.power=this.power-blood;
    }
    public void showInfo(){
        if(this.power<=0){
            System.out.println(this.name+"over");
        }else {
            System.out.println(this.name+"的血量"+this.power);
        }
        
    }
    
    

}

 

类之间的关系练习2

标签:bow   power   class   使用   setw   system   nbsp   span   ted   

原文地址:http://www.cnblogs.com/zyy1130/p/6723715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!