Java继承和接口的使用实例

   日期:2020-07-18     浏览:97    评论:0    
核心提示:一个游戏角色案例,通过接口,实现游戏角色的职业、武器、伤害角色行为接口(用于角色的攻击和修炼方法)package com.hope.zuoye2;public interface JiaoSe {//角色接口 int attack(WuQi type);//攻击方法,返回对敌人的伤害值 void practise();//修炼方法,练习后会提升金币}角色抽象类(实现角色行为的接口)package com.hope.zuoye2;public abstract clas.

一个游戏角色案例,通过接口,实现游戏角色的职业、武器、伤害

角色行为接口(用于角色的攻击和修炼方法)

package com.hope.zuoye2;

public interface JiaoSe {//角色接口
    int attack(WuQi type);//攻击方法,返回对敌人的伤害值
    void practise();//修炼方法,练习后会提升金币
}

角色抽象类(实现角色行为的接口)

package com.hope.zuoye2;

public abstract class NameRole implements JiaoSe {
    String name;
    int age;
    String career;
    public NameRole(String name, int age, String career) {
        this.name = name;
        this.age = age;
        this.career = career;
    }
    public NameRole() {
    }
    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 String getCareer() {
        return career;
    }
    public void setCareer(String career) {
        this.career = career;
    }
    @Override
    public String toString() {
        return "NameRole{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", career='" + career + '\'' +
                '}';
    }
    @Override
    public int attack(WuQi type) {//返回值为角色的攻击对敌人的伤害
        return 0;
    }
    @Override
    public void practise() {//练习方法,练习后会提升金币
    }
}

战士类(实现角色抽象类)

package com.hope.zuoye2;

import javax.sound.midi.Soundbank;
import java.sql.SQLOutput;

public class Soldier extends NameRole {    //战士类
    int money;//金币
    private int hurt;//伤害
    int count=0;//角色修炼的次数

    public Soldier(String name, int age, String career, int money, int hurt) {
        super(name, age, career);
        this.money = money;
        this.hurt = hurt;
    }

    public Soldier() {
    }
    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public int getHurt() {
        return hurt;
    }

    public void setHurt(int hurt) {
        this.hurt = hurt;
    }


    @Override
    public int attack(WuQi type) {//返回值为角色的攻击对敌人的伤害
        //角色购买装备 伤害增加 调用购买武器的方法
        int armshurt = arms(type);
        return armshurt;
    }

    @Override
    public void practise() {//练习方法,练习后会提升金钱
        //当金币大于500时 并积累了10次修炼就不可以再修炼 每次修炼金币在原有的基础上+30
        if(money<500){
            if(count<10){
                money+=30;
                count++;
                System.out.println("修炼后金钱为:"+money);
            }else {
                System.out.println("您今天的修炼次数太频繁了,请明天再来修炼");
            }

         }else {
            System.out.println("您的金币已达到上限不能在修炼了");
        }

    }

    //购买武器的方法
    public int arms(WuQi type) {//将武器的属性作为参数传过来
        //当角色的金币大于装备需要的金币时才可以购买
        if (money> type.getMoney()){
            //叠加伤害 =原有伤害+物理武器攻击力的10%
                hurt+=(type.getAttackPower()*0.10);
                System.out.println("你成功购买装备"+type.getName());
                money-=type.getMoney();

            }else {
                System.out.println("余额不足");
            }
        return hurt;
    }

    @Override
    public String toString() {
        return "Soldier{" +
                "money=" + money +
                ", hurt=" + hurt +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", career='" + career + '\'' +
                '}';
    }
}

武器类

package com.hope.zuoye2;

public class WuQi {
    private String name;//武器名字
    private int money;//花费金币
    private int attackPower;//攻击力
    private int spellPower;//法术攻击力

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

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public int getAttackPower() {
        return attackPower;
    }

    public void setAttackPower(int attackPower) {
        this.attackPower = attackPower;
    }

    public int getSpellPower() {
        return spellPower;
    }

    public void setSpellPower(int spellPower) {
        this.spellPower = spellPower;
    }

    public WuQi(String name, int money, int attackPower, int spellPower) {
        this.name = name;
        this.money = money;
        this.attackPower = attackPower;
        this.spellPower = spellPower;
    }

    public WuQi() {
    }
}

法师类(同战士类)

package com.hope.zuoye2;

public class Magicer extends  NameRole {

    int money;
    private int hurt;//伤害

    int count=0;

    public Magicer(String name, int age, String career, int money, int hurt) {
        super(name, age, career);
        this.money = money;
        this.hurt = hurt;
    }

    public Magicer() {
    }
    
    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public int getHurt() {
        return hurt;
    }

    public void setHurt(int hurt) {
        this.hurt = hurt;
    }

    @Override
    public int attack(WuQi type) {//返回值为角色的攻击对敌人的伤害
        int armshurt = arms(type);
        return armshurt;
    }

    @Override
    public void practise() {//练习方法,练习后会提升金钱
        if(money<500){
            if(count<10){
                money+=30;
                count++;
                System.out.println("修炼后金钱为:"+money);
            }else {
                System.out.println("您今天的修炼次数太频繁了,请明天再来修炼");
            }

        }else {
            System.out.println("您的金币已达到上限不能在修炼了");
        }

    }

    public int arms(WuQi type) {
        //叠加伤害 =原有伤害+法术武器攻击力的25%
        if (money> type.getMoney()){
            hurt+=(type.getSpellPower()*0.25);
            System.out.println("你成功购买装备"+type.getName());
            money-=type.getMoney();

        }else {
            System.out.println("余额不足");
        }
        return hurt;
    }

    @Override
    public String toString() {
        return "Magicer{" +
                "money=" + money +
                ", hurt=" + hurt +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", career='" + career + '\'' +
                '}';
    }
}

测试类

package com.hope.zuoye2;

import java.util.List;
import java.util.Scanner;

public class TestNameRole {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        boolean bn=false;
        NameRole nameRole=null;
        int enterCount=3;//允许重新输入的次数
        while(!bn){//循环
        System.out.println("1.创建角色,2.购买装备3.修炼 4.退出");
        if (sc.hasNextInt()){
            int num=sc.nextInt();
            switch (num){
                case 1:
                    System.out.println("请输入角色名称:");
                    String name = sc.next();
                    System.out.println("请输入角色年龄:");
                    int age=sc.nextInt();
                    System.out.println("请输入角色职业:");
                    String career=sc.next();

                    if(career.equals("战士")){
                        nameRole = new Soldier(name,age,career,300,30);
                    }
                    else if (career.equals("法师")){
                        nameRole=new Magicer(name,age,career,340,60);
                    }
                    else if (!career.equals("战士") || !career.equals("法师")){
                        System.out.println("没有该职业");
                    }
                    System.out.println("请按n返回上一级");
                    String n=sc.next();
                    if(!n.equals("n")){
                        bn=true;
                    }
                    break;

                case 2:
                    //购买装备 当前对象不为null时 允许购买
                    if (nameRole!=null){
                        if (nameRole.getCareer().equals("战士")){
                            WuQi wuQi = new WuQi("无尽战刃", 120, 150, 0);
                            int attack = nameRole.attack(wuQi);
                            System.out.println(nameRole.toString()+"现在物理伤害:"+attack);
                        }else {
                            WuQi wuQi = new WuQi("回响之杖", 130, 0, 160);
                            int attack = nameRole.attack(wuQi);
                            System.out.println(nameRole.toString()+"现在法术伤害:"+attack);
                        }

                    }else {
                        System.out.println("请先创建角色,再购买装备");
                    }
                    break;
                case 3:

                    if(nameRole!=null){
                        //当前对象不为null时 调用修炼方法
                            nameRole.practise();
                    }else {
                        System.out.println("请先创建角色,再修炼");
                    }

                    break;
                case 4:
                    //退出
                        bn=true;
                    break;

                default:
                    if (enterCount>1){//当enterCount允许重新输入的次数大于1时执行
                        enterCount--;
                        System.out.println("输入错误,请重新输入还有"+enterCount+"机会");
                    }else {
                    System.out.println("请重新运行程序");
                        bn=true;
                    }
                    break;
            }
        }
        }
    }
}
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服