奥特曼打小怪兽
定义奥特曼类:
属性:a.奥特曼名字
b.奥特曼生命值
c.奥特曼魔法值
方法: a.奥特曼简单攻击
b.奥特曼救济必杀攻击
c.奥特曼魔法攻击(一群小怪兽受伤害)
“`Java
package com.lovoinfo;
/**
* 奥特曼
* @author Administrator
*
*/
public class Ultraman {
private String name;// 名字
private int hp;// 生命值
private int mp;// 魔法值
public Ultraman(String name) {
this.name = name;
this.hp=500;
this.mp=100;
}
public int getMp() {
return mp;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp > 0? hp : 0;
}
/**
* 攻击
*/
public void attack(Monster m) {
int injury=(int) (Math.random()*21+10);//10-30的随机攻击
m.setHp(m.getHp()-injury);
}
/**
* 究极必杀技
* @param m
*/
public void hugeAttack(Monster m){
m.setHp(m.getHp()-50);
}
/**
* 对一群怪兽使用魔法攻击
*/
public void magicAttack(Monster[] ms) {
for(int i=0;i<ms.length;i++){
Monster m=ms[i];
this.mp -= 30;
if(m.isAlive()){
m.setHp(m.getHp()-20);
}else{
m.setHp(0);
}
}
}
public String toString(){
return this.name+"的生命值还有:"+this.hp;
}
}
定义类小怪兽Monste