蜈蚣精类:
属性包括:怪物名字,生命值,攻击力
方法包括:攻击,移动(飞行移动)
要求
⦁分析蛇怪和蜈蚣精的公共成员,提取出父类—怪物类
⦁利用继承机制,实现蛇怪类和蜈蚣精类
⦁攻击方法,描述攻击状态。内容包括怪物名字,生命值,攻击力
⦁编写测试类,分别测试蛇怪和蜈蚣精的对象及相关方法
⦁定义名为mon的包存怪物类,蛇怪类,蜈蚣精类和测试类
运行效果:
首先根据题目写出父类。有名字,血量,攻击力等私有属性,攻击和移动的方法,还有一个显示状态的输出方法。一个有参构造,在子类构造方法时可以通过super关键字使用父类的这个方法。
父类:
class Monster{
private String name;
private int blood;
private int force;
public Monster(String name2, int blood2, int force2) {
this.name=name2;
this.blood=blood2;
this.force=force2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getForce() {
return force;
}
public void setForce(int force) {
this.force = force;
}
void show() {
System.out.println("当前生命值是:"+getBlood());
System.out.println("攻击力是:"+getForce());
}
void attack(){
System.out.println("怪物发动攻击");
}
void Move(){
System.out.println("直线移动");
}
}
蛇精类:
class snakeMonster extends Monster{
public snakeMonster(String name,int blood,int force){
super(name,blood,force);
}
void Move(){
System.out.println("我是蛇怪,我走S型路线");
}
void recover() {
int remainblood=getBlood();
remainblood=remainblood<10?remainblood+20:remainblood;
System.out.println("实施大蛇补血术。。。。。,当前生命值是:"+remainblood);
}
void attack(){
System.out.println(getName()+"展开攻击");
}
}
蜈蚣精类:
class centipedeMonster extends Monster{
public centipedeMonster(String name,int blood,int force){
super(name,blood,force);
}
void Move() {
System.out.println("我是蜈蚣精,御风飞行");
}
void attack() {
System.out.println(getName()+"展开攻击");
}}
测试类:
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
snakeMonster snake=new snakeMonster("蛇精甲",5,20);
snake.attack();
snake.show();
snake.recover();
snake.Move();
System.out.println("--------------------------------");
centipedeMonster centipede=new centipedeMonster("蜈蚣乙",60,15);
centipede.attack();
centipede.show();
centipede.Move();
}}
完整代码:
package mon;
class Monster{
private String name;
private int blood;
private int force;
public Monster(String name2, int blood2, int force2) {
this.name=name2;
this.blood=blood2;
this.force=force2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getForce() {
return force;
}
public void setForce(int force) {
this.force = force;
}
void show() {
System.out.println("当前生命值是:"+getBlood());
System.out.println("攻击力是:"+getForce());
}
void attack(){
System.out.println("怪物发动攻击");
}
void Move(){
System.out.println("直线移动");
}
}
class snakeMonster extends Monster{
public snakeMonster(String name,int blood,int force){
super(name,blood,force);
}
void Move(){
System.out.println("我是蛇怪,我走S型路线");
}
void recover() {
int remainblood=getBlood();
remainblood=remainblood<10?remainblood+20:remainblood;
System.out.println("实施大蛇补血术。。。。。,当前生命值是:"+remainblood);
}
void attack(){
System.out.println(getName()+"展开攻击");
}
}
class centipedeMonster extends Monster{
public centipedeMonster(String name,int blood,int force){
super(name,blood,force);
}
void Move() {
System.out.println("我是蜈蚣精,御风飞行");
}
void attack() {
System.out.println(getName()+"展开攻击");
}
}
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
snakeMonster snake=new snakeMonster("蛇精甲",5,20);
snake.attack();
snake.show();
snake.recover();
snake.Move();
System.out.println("--------------------------------");
centipedeMonster centipede=new centipedeMonster("蜈蚣乙",60,15);
centipede.attack();
centipede.show();
centipede.Move();
}
}
运行效果: