超级烧脑的java接口扩展练习题(封装+继承+多态+实现+相互调用+多个实现类)

发布时间:2023年12月27日

———完成以下游戏角色内容————封装、继承、多态—————————

注:某些方法的形参列表、返回值可以根据情况调整,并非固定不可变

父类:Role(角色),是所有职业的父类
1)属性:name,表示角色的名字
2)方法:public int attack(),该方法返回值为角色的攻击对敌人的伤害

1:Role类

package com.project.game;

public abstract class Role {
    private String name;
    public Role(){

    }

    public Role(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public abstract int attack();
}

Role的第一个子类:Magicer(法师)
1)属性:魔法等级(范围为1~10)
2)方法:public int attack(),该方法返回法师的攻击对敌人造成的伤害值
? ? ? 法师攻击伤害值为:魔法等级*魔法基本伤害值(固定为5)

2:Magicer类

package com.project.game;

public class Magicer extends Role {
    private int magicLevel;

    public Magicer() {
    }

    public Magicer(int magicLevel) {
        this.magicLevel = magicLevel;
    }

    public int getMagicLevel() {
        return magicLevel;
    }

    public void setMagicLevel(int magicLevel) {
        this.magicLevel = magicLevel;
    }

    public int attack(){
        int shanghai=5;
        int num=magicLevel*shanghai;
        System.out.println("法师攻击伤害值为:"+num);
        return num;
    }
}

Role的第二个子类:Soldier(战士)
1)属性:攻击伤害值
2)方法:public int attack(),该方法返回战士对敌人造成的伤害值
? ? ? 战士的攻击伤害值为:其攻击伤害属性值

3:Soldier类

package com.project.game;

import com.project.game.Role;

public class Soldier extends Role {
    private int attackDamage;
    public Soldier(){

    }
    public Soldier(int attackDamage){
        this.attackDamage=attackDamage;
    }

    public int getAttackDamage(){
        return attackDamage;
    }
    public void setAttackDamage(int attackDamage){
        this.attackDamage=attackDamage;
    }

    public int attack(){
        System.out.println("战士攻击伤害值为:"+attackDamage);
        return attackDamage;
    }
}

再设计一个Team类,表示一个组队。
1)addMember,表示组队增加一个成员
? ? ? 注意:组队成员最多为6人
? ? ? 提示:应当利用一个数组属性,保存所有成员
2)attackSum,表示组队所有成员进行攻击时,对敌人造成的总伤害

4:Team类

package com.project.game;

import com.project.game.Magicer;
import com.project.game.Role;

public class Team {
    Role[]role;
    int count=0;
    public void addMember(){
        if(count<6){
            count++;
            role=new Role[count];
        }
    }


    int num=0;
    public void addMembers(Role roles){
        if(num<role.length){

            role[num]=roles;
            num++;

        }
    }
//   public void addMembers(Role role1){
//       for(int i=1;i<role.length;i++){
//           role[i]=role1;
//       }
//   }


    public int attackSum(){
        int zongshanghai=0;
        for(int i=0;i<role.length;i++){
            if(role[i] instanceof Magicer){
               zongshanghai+=role[i].attack();
            }else{
                zongshanghai+=role[i].attack();
            }
        }
        return zongshanghai;
    }

}

?5:Test测试类

package com.project.game;

import com.project.game.Magicer;
import com.project.game.Soldier;
import com.project.game.Team;

public class Test {
    public static void main(String[]args){
        Team team=new Team();
        team.addMember();
        team.addMember();
        team.addMember();
        team.addMember();
        team.addMember();
        team.addMember();
        System.out.println("count值为:"+team.count);

        team.addMembers(new Magicer(2));
        team.addMembers(new Magicer(2));
        team.addMembers(new Magicer(2));
        team.addMembers(new Magicer(2));
        team.addMembers(new Magicer(2));
        team.addMembers(new Soldier(100));
        System.out.println("数组长度为:"+team.role.length);
//        for(int i=0;i<team.role.length;i++){
//            System.out.println(i+"team.role[i]");
//        }



//        team.addMembers(new Soldier(10));
//        team.addMembers(new Soldier(10));
//        team.addMembers(new Soldier(10));
//        team.addMembers(new Soldier(10));
//        team.addMembers(new Soldier(10));
//        team.addMembers(new Soldier(10));

        int num=team.attackSum();
        System.out.println("总伤害为:"+num);

    }
}


—————在上述代码基础之上完成以下需求——封装、继承、多态、接口—————————


创建Role接口,包含两个方法:
1)int attack(); 表示攻击,返回值表示对敌人的伤害
2)void practise(); 表示练习。练习之后对敌人的伤害会增加

6:Role接口

package com.project.gameInterface;

public interface Role {
    int attack();
    void practise();
}

创建NamedRole类,该类为一个抽象类,实现了Role 接口,并有两个属性:
1)name:角色的名字
2)age:角色的年龄

7:NamedRole抽象类

package com.project.gameInterface;

public  abstract class NamedRole implements Role{
    private String name;
    private int age;

    public NamedRole() {
    }

    public NamedRole(String name, int age) {
        this.name = name;
        this.age = age;
    }

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


}

增加MagicStick接口。该接口表示法师使用的法杖。接口中包含一个方法:
1)int fire()

8:MagicStick接口

package com.project.gameInterface;

public interface MagicStick {
    int fire();
}

为MagicStick接口增加两个实现类,分别为GreenStick和BlackStick。其中,对于这两个类的fire方法:
1)GreenStick 平时返回1,夏天(6~8月)使用时返回2
2)BlackStic奇数月返回1,偶数月返回2

9:GreenStick类

package com.project.gameInterface;

import java.time.LocalDate;
import java.time.YearMonth;

public class GreenStick implements MagicStick{

    LocalDate currentDate = LocalDate.now();
    YearMonth yearMonth = YearMonth.from(currentDate);
    int month = yearMonth.getMonthValue();

    @Override
    public int fire() {
        System.out.println("当前月份: " + month);
        if(month>=6&&month<=8){
            System.out.println("六月到八月返回2");
            return 2;
        }else {
            System.out.println("非六到八月返回1");
            return 1;
        }
    }
}

10:BlackStick类

package com.project.gameInterface;

import java.time.LocalDate;
import java.time.YearMonth;

public class BlackStick implements MagicStick{
    LocalDate currentDate = LocalDate.now();
    YearMonth yearMonth = YearMonth.from(currentDate);
    int month = yearMonth.getMonthValue();


    @Override
    public int fire() {
        System.out.println("当前月份: " + month);
        if(month%2==0){
            System.out.println("偶数月返回2");
            return 2;
        }else{
           System.out.println("奇数月返回1");
            return 1;
        }
    }
}

修改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方法的返回值

11:修改Magicer类

package com.project.gameInterface;


public class Magicer extends NamedRole{
    private int magicLevel;

    private  MagicStick stick;


    public Magicer() {
    }

    public Magicer(int magicLevel,MagicStick stick) {
        this.magicLevel = magicLevel;
        this.stick=stick;
    }

    public int getMagicLevel() {
        return magicLevel;
    }

    public void setMagicLevel(int magicLevel) {
        this.magicLevel = magicLevel;
    }

    public MagicStick getStick() {
        return stick;
    }

    public void setStick(MagicStick stick) {
        this.stick = stick;
    }

    public int attack(){
        int shanghai=5;
        int num=magicLevel*shanghai;
        System.out.println("法师攻击伤害值为:"+num);
        return num;
    }

    @Override
    public void practise() {
        if(stick==null){
            magicLevel++;
        }else{
            magicLevel+=1+stick.fire();
        }
        System.out.println("法师等级为:"+magicLevel);
    }

}

增加Weapon接口,表示战士使用的武器。Weapon接口中定义了两个方法:
1)void setSoldier(Soldier s); 该方法表示设置武器的使用者
2)int fire(); 该方法的返回值表示战士使用该武器时,对敌人的伤害值

12:Weapon接口

package com.project.gameInterface;

public interface Weapon {
    //表示战士使用的武器
    void setSoldier(Soldier s);
    int fire();
}

为 Weapon 增加两个实现了,Bolo,表示大刀,Pike,表示长矛
1)当 soldier 的年龄大于等于 18 岁时,fire 方法返回 100
2)当 soldier 年龄小于 18 岁时,fire 方法返回 50

13:Bole类

package com.project.gameInterface;

public class Bolo implements Weapon{
    //大刀实现类
    @Override
    public void setSoldier(Soldier s) {
        if(s.getAge()>=18){
            int a=fire();
            System.out.println("大于18返回值为"+a);
        }else {
            int b=fire1();
            System.out.println("小于18返回值为"+b);
        }
    }

    @Override
    public int fire() {
        return 100;
    }
    public int fire1() {
        return 50;
    }
}

Pike:Pike 类有一个属性:name,表示长矛的名字
1)当长矛的名字和战士的名字一致时,fire 方法返回 1000
2)当长矛的名字和战士的名字不一致时,fire 方法返回 25

14:Pick类

package com.project.gameInterface;

public class Pick implements Weapon{
    //长矛实现类
    private String name;

    public Pick() {
    }

    public Pick(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setSoldier(Soldier s) {
        if(s.getName().equals(name)){
            int a=fire();
            System.out.println("名字相等时为:"+a);
        }else{
            int b=fire1();
            System.out.println("名字不相等时为:"+b);
        }

    }

    @Override
    public int fire() {
        return 1000;
    }
    public int fire1() {
        return 25;
    }


//    public int fire() {
//        if(soldier.getName().equals(name)){
//            System.out.println("名字相等时为:"+1000);
//            return 1000;
//        }else {
//            System.out.println("名字不相等时为:"+25);
//            return 25;
//        }
//
//    }
}

修改 Soldier 类
1)为 Soldier 类增加一个 Weapon 属性,表示战士的武器
2)让其继承自 NamedRole 类,并实现 attack 和 practise 功能。其中

? ? Soldier 的 attack() 返回值为战士的 hurt 值与武器的 fire()返回值的 和,即:总攻击输出 = 战士的徒手伤害值 + 武器的伤害值
? ? practise()方法:每调用一次则战士的 hurt 值+10

15:修改Soldirer类

package com.project.gameInterface;

import sun.security.mscapi.SunMSCAPI;

public class Soldier extends NamedRole {
    private Weapon weapon;
    private String name;
    private int age;
    private int attackDamage;

    public Soldier() {

    }

    public Soldier(String name) {
        this.name = name;
    }

    public Soldier(int age) {
        this.age = age;
    }

    public Soldier(String name, int age, int attackDamage) {
        this.name = name;
        this.age = age;
        this.attackDamage = attackDamage;
    }

    public Soldier(Weapon weapon, String name, int age, int attackDamage) {
        this.weapon = weapon;
        this.name = name;
        this.age = age;
        this.attackDamage = attackDamage;
    }

    public Soldier(String name, int age, Weapon weapon, String name1, int age1, int attackDamage, int sum) {
        super(name, age);
        this.weapon = weapon;
        this.name = name1;
        this.age = age1;
        this.attackDamage = attackDamage;
        this.sum = sum;
    }

    public Weapon getWeapon() {
        return weapon;
    }

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

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

    public int getAttackDamage() {
        return attackDamage;
    }

    public void setAttackDamage(int attackDamage) {
        this.attackDamage = attackDamage;
    }

    @Override
    public void practise() {
        attackDamage+=10;
    }

    int sum=0;
    public int attack() {
        if (weapon instanceof Bolo){
            weapon=new Bolo();
            if(age>18){
                sum = attackDamage + weapon.fire();
                System.out.println("战士攻击伤害值为:" + sum);
            }else {
                sum = attackDamage + ((Bolo) weapon).fire1();
                System.out.println("战士攻击伤害值为:" + sum);
            }

            return sum;
        }else {
            weapon=new Pick("张三");
            Pick p=(Pick)weapon;
            if(p.getName().equals(name)){
                sum=attackDamage+weapon.fire();
                System.out.println("战士攻击伤害值为:" + sum);
            }else {
                sum=attackDamage+((Pick) weapon).fire1();
                System.out.println("战士攻击伤害值为:" + sum);
            }

            return sum;
        }
    }

}

16:测试类Test

package com.project.gameInterface;

public class Test {
    public static void main(String[]args){
//        BlackStick b=new BlackStick();
//        GreenStick g=new GreenStick();
//        int num=g.fire();
//        System.out.println(num);
//        System.out.println(b.fire());



        Magicer magicer=new Magicer(10,new BlackStick());
        magicer.practise();
        Magicer m=new Magicer(10,new GreenStick());
        m.practise();

//        Magicer mg=new Magicer(10,);
//        mg.practise();
        System.out.println("_______________________________________");

        Weapon b=new Bolo();
        b.setSoldier(new Soldier(20));



        Weapon pick=new Pick("张三");
        pick.setSoldier(new Soldier("张三"));
        pick.setSoldier(new Soldier("李四"));



        System.out.println("-------------------------------------");


        Soldier soldier=new Soldier(new Bolo(),"张三",120,10);
        new Bolo().setSoldier(new Soldier(17));
        soldier.practise();
        soldier.practise();
        soldier.practise();
        soldier.attack();
        new Bolo().setSoldier(new Soldier(20));
        soldier.attack();

        System.out.println("--------------------------------");

        Soldier soldier1=new Soldier(new Pick("张三"),"张三",18,10);
        new Pick("李四").setSoldier(new Soldier("张三"));
        soldier1.practise();
        soldier1.practise();
//        new Pick("张三").fire();
        soldier1.attack();

        Soldier soldier2=new Soldier(new Pick("张三"),"张三",18,10);
        new Pick("张三").setSoldier(new Soldier("张三"));
        soldier1.attack();

    }
}

基本功能已经实现,可能会有部分功能可以编译运行,但是运行结果有误,不会报错

下面是原题

谢谢你的观看

文章来源:https://blog.csdn.net/zhzjn/article/details/135247143
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。