Java(封装、继承、多态、接口)游戏中的角色练习题
游戏中的角色练习题
题目
创建Role接口,包含两个方法:
1)int attack(); 表示攻击,返回值表示对敌人的伤害
2)void practise(); 表示练习。练习之后对敌人的伤害会增加
创建NamedRole类,该类为一个抽象类,实现了Role 接口,并有两个属性:
1)name:角色的名字
2)age:角色的年龄
增加MagicStick接口。该接口表示法师使用的法杖。接口中包含一个方法:
1)int fire()
为MagicStick类增加两个实现类,分别为GreenStick和BlackStick。其中,对于这两个类的fire方法:
1)GreenStick 平时返回1,夏天(6~8月)使用时返回2
2)BlackStic奇数月返回1,偶数月返回2
修改Magicer类
1)为法师类增加MagicStick类的属性stick,表示法师使用的法杖
2)让其继承自NamedRole类,并实现attack和practise 功能
3)int attack()返回值为法师的魔法等级(level)*每一级的固定伤害(5)
4)void practise()方法:
当法师的stick属性为null时,调用 practise则level++
当法师的stick不为null时,调用practise 方法时,法师的等级level满足:level = level + 1 + stick.fire();即:法师的等级增加为1+stick 属性的fire方法的返回值
增加Weapon接口,表示战士使用的武器。Weapon接口中定义了两个方法:
1)void setSoldier(Soldier s); 该方法表示设置武器的使用者
2)int fire(); 该方法的返回值表示战士使用该武器时,对敌人的伤害值
为 Weapon 增加两个实现了,Bolo,表示大刀,Pike,表示长矛
1)当 soldier 的年龄大于等于 18 岁时,fire 方法返回 100
2)当 soldier 年龄小于 18 岁时,fire 方法返回 50
Pike:Pike 类有一个属性:name,表示长矛的名字
1)当长矛的名字和战士的名字一致时,fire 方法返回 1000
2)当长矛的名字和战士的名字不一致时,fire 方法返回 25
修改 Soldier 类
1)为 Soldier 类增加一个 Weapon 属性,表示战士的武器
2)让其继承自 NamedRole 类,并实现 attack 和 practise 功能。其中
Soldier 的 attack() 返回值为战士的 hurt 值与武器的 fire()返回值的 和,即:总攻击输出 = 战士的徒手伤害值 + 武器的伤害值
practise()方法:每调用一次则战士的 hurt 值+10
编写相应的测试代码
————————————————
版权声明:本文为CSDN博主「Ziph」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44170221/article/details/104574963
思维导图
相关代码
接口,抽象方法
package com.company1.interfacep;
/*
* 表示法师使用的法杖
* */
public interface MagicStick {
int fire();//
}
package com.company1.interfacep;
import com.company1.extendsl.Soldier;
public interface Weapon {
void setSoldier(Soldier s); //该方法表示设置武器的使用者
int fire(); //该方法的返回值表示战士使用该武器时,对敌人的伤害值
}
package com.company1.interfacep;
public interface Role {
int attack();// 表示攻击,返回值表示对敌人的伤害
void practise();//表示练习。练习之后对敌人的伤害会增加
}
package com.company1.impl;
import com.company1.interfacep.Role;
public abstract class NamedRole implements Role {
private String name;//角色的名字
private int age;//角色的年龄
public NamedRole(String name, int age) {
this.name = name;
this.age = age;
}
public NamedRole(int age) {
this.age = age;
}
public NamedRole(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "NamedRole{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public NamedRole() {
}
}
接口实现
package com.company1.impl;
import com.company1.interfacep.MagicStick;
import java.util.Date;
public class BlackStic implements MagicStick {
Date d = new Date();
@Override
public int fire() {
if (d.getMonth() % 2 == 0) {
return 2;
} else {
return 1;
}
}
}
package com.company1.impl;
import com.company1.extendsl.Soldier;
import com.company1.interfacep.Weapon;
public class Bolo implements Weapon {
private int age;
@Override
public void setSoldier(Soldier s) {
age = s.getAge();
}
@Override
public int fire() {
if(age>=18){
return 100;
}else{
return 50;
}
}
}
package com.company1.impl;
import com.company1.interfacep.MagicStick;
import java.util.Date;
public class GreenStick implements MagicStick {
Date d=new Date();
@Override
public int fire() {
if(d.getMonth()>=6&&d.getMonth()<=8){
return 2;
}else{
return 1;
}
}
}
package com.company1.impl;
import com.company1.extendsl.Soldier;
import com.company1.interfacep.Weapon;
public class Pike implements Weapon {
private Soldier s;
private String name;
public Pike(String name) {
this.name = name;
}
public int fire() {
s = new Soldier(name);
if (s.getName().equals(name)) {
return 1000;
} else {
return 25;
}
}
public void setSoldier(Soldier s) {
this.s = s;
}
}
抽象类实现
package com.company1.extendsl;
import com.company1.impl.NamedRole;
import com.company1.interfacep.MagicStick;
public class Magicer extends NamedRole {
private int level=1;//法师的魔法等级
private MagicStick stick;//表示法师使用的法杖
public int getLevel() {
return level;
}
public void setLevel(int level) {
if (level < 1 || level > 10) {
System.out.println("level error!");
return;
}
this.level = level;
}
public MagicStick getStick() {
return stick;
}
public void setStick(MagicStick stick) {
this.stick = stick;
}
@Override
public String toString() {
return "Magicer{" +
"level=" + level +
", stick=" + stick +
'}';
}
public Magicer(String name, int age) {
super(name, age);
}
public Magicer(String name, int age, int level) {
super(name, age);
this.level = level;
}
public Magicer(int level, MagicStick stick) {
this.level = level;
this.stick = stick;
}
public Magicer() {
}
@Override
public int attack() {
return level*5;
}
@Override
public void practise() {
if(stick==null){
level++;
}else{
level = level + 1 + stick.fire();//法师的等级增加为1+stick 属性的fire方法的返回值
}
}
}
package com.company1.extendsl;
import com.company1.impl.NamedRole;
import com.company1.interfacep.Weapon;
public class Soldier extends NamedRole {
private int hurt;
private Weapon weapon;
public Soldier(String name) {
super(name);
}
public Soldier(int age) {
super(age);
}
public Soldier(String name, int age, int hurt) {
super(name, age);
this.hurt = hurt;
}
public int getHurt() {
return hurt;
}
public void setHurt(int hurt) {
this.hurt = hurt;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
@Override
public int attack() {
int result = hurt;
if (weapon != null) {
result += weapon.fire();
}
return result;
}
@Override
public void practise() {
hurt += 10;
}
}
测试类
package com.company1;
import com.company1.extendsl.Magicer;
import com.company1.extendsl.Soldier;
import com.company1.impl.BlackStic;
import com.company1.impl.Bolo;
import com.company1.impl.GreenStick;
import com.company1.impl.Pike;
import com.company1.interfacep.MagicStick;
import com.company1.interfacep.Weapon;
public class Test {
public static void main(String[] args) {
MagicStick magicStick1 = new GreenStick();
MagicStick magicStick2 = new BlackStic();
Magicer magicer1 = new Magicer( "基拉法师",21, 61);
Magicer magicer2 = new Magicer("光鳞龙",15, 5);
magicer1.setStick(magicStick1);
magicer2.setStick(magicStick2);
System.out.println(magicer1.getName() + "\t" + "年龄:" + magicer1.getAge() + "\t" + "初始攻击伤害:" + magicer1.attack());
System.out.println(magicer2.getName() + "\t" + "年龄:" + magicer2.getAge() + "\t" + "初始攻击伤害:" + magicer2.attack());
magicer1.practise();
magicer2.practise();
System.out.println(magicer1.getName() + "\t" + "年龄:" + magicer1.getAge() + "\t" + "修炼后的攻击伤害:" + magicer1.attack());
System.out.println(magicer2.getName() + "\t" + "年龄:" + magicer2.getAge() + "\t" + "修炼后的攻击伤害:" + magicer2.attack());
System.out.println("-----------------------------------------------------------------");
Soldier soldier1 = new Soldier("Ziph", 21, 200);
Soldier soldier2 = new Soldier("狂战士", 30, 150);
Weapon weapon1 = new Bolo();
Weapon weapon2 = new Pike("Ziph");
soldier1.setWeapon(weapon1);
soldier1.setWeapon(weapon2);
soldier2.setWeapon(weapon1);
soldier2.setWeapon(weapon2);
System.out.println(soldier1.getName() + "\t" + "年龄:" + soldier1.getAge() + "\t" + "佩戴了大刀和矛之后的攻击伤害:" + soldier1.attack());
System.out.println(soldier2.getName() + "\t" + "年龄:" + soldier2.getAge() + "\t" + "佩戴了大刀和矛之后的攻击伤害:" + soldier2.attack());
soldier1.practise();
soldier2.practise();
System.out.println(soldier1.getName() + "\t" + "年龄:" + soldier1.getAge() + "\t" + "佩戴了大刀和矛,加以修炼后的攻击伤害:" + soldier1.attack());
System.out.println(soldier2.getName() + "\t" + "年龄:" + soldier2.getAge() + "\t" + "佩戴了大刀和矛,加以修炼后的攻击伤害:" + soldier2.attack());
}
}
代码实现
上一篇: C#学习笔记(七)数组的基本用法