• 【Q】类和对象:游戏角色开发(角色介绍)


    案例情景:某公司要开发新游戏,请用面向对象的思想设计英雄类、怪物类和武器类。

      编写测试类,创建英雄对象、怪物对象和武器对象,并输出各自的信息。

      其中设定分别如下:

      1.英雄类

        属性:英雄名字、生命值。

        方法:输出基本信息。

      2、怪物类

        属性:怪物名字、生命值、类型。

        方法:输出基本信息。

      3.武器类

        属性:武器名字、攻击力。

        方法:输出基本信息。

    一、创建实体类

      1.我们已经学过抽象类和继承,所有我们可以从高一层的角度来设计实体类(首先根据各种角色所具有的共同的特征抽象出一个抽象类)

        抽象的角色类:

     1 /**
     2  * 游戏角色:抽象类
     3  * 
     4  * @author Administrator
     5  *
     6  */
     7 public abstract class Role {
     8     private String role;// 角色名称
     9     private String name;// 角色姓名
    10     // 封装属性
    11 
    12     public String getRole() {
    13         return role;
    14     }
    15 
    16     public void setRole(String role) {
    17         this.role = role;
    18     }
    19 
    20     public String getName() {
    21         return name;
    22     }
    23 
    24     public void setName(String name) {
    25         this.name = name;
    26     }
    27 
    28     // 介绍方法
    29     public abstract void showInfo();
    30 }

      2.继承抽象类,创建三个具体的角色类

        (1)英雄类

     1 /**
     2  * 英雄类
     3  * 
     4  * @author Administrator
     5  *
     6  */
     7 public class Hero extends Role {
     8     private int health;// 健康值
     9 
    10     public Hero() {
    11         super();
    12         this.setRole("英雄");
    13         this.setName("李小侠");
    14         this.setHealth(300);
    15     }
    16 
    17     @Override
    18     public void showInfo() {
    19         System.out.println("我是" + this.getRole() + ",我的基本信息如下:
    姓名:" + this.getName() + ",生命值:" + this.getHealth());
    20     }
    21 
    22     public int getHealth() {
    23         return health;
    24     }
    25 
    26     public void setHealth(int health) {
    27         this.health = health;
    28     }
    29 
    30 }

        (2)武器类

    /**
     * 武器类
     * 
     * @author Administrator
     *
     */
    public class Weapons extends Role {
        private int attack;// 攻击力
    
        public Weapons() {
            super();
            this.setRole("武器");
            this.setName("死亡镰刀");
            this.setAttack(12);
        }
    
        @Override
        public void showInfo() {
            System.out.println("我是" + this.getRole() + ",我的基本信息如下:
    武器名:" + this.getName() + ",攻击力:" + this.getAttack());
        }
    
        public int getAttack() {
            return attack;
        }
    
        public void setAttack(int attack) {
            this.attack = attack;
        }
    
    }

        (3)怪物类

    /**
     * 怪物类
     * 
     * @author Administrator
     *
     */
    public class Monster extends Role {
        private int health;// 健康值
        private String type;// 类型
    
        public Monster() {
            super();
            this.setRole("怪物");
            this.setName("小龟");
            this.setHealth(300);
            this.setType("潜水类");
        }
    
        public int getHealth() {
            return health;
        }
    
        public void setHealth(int health) {
            this.health = health;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        @Override
        public void showInfo() {
            System.out.println("我是" + this.getRole() + ",我的基本信息如下:
    姓名:" + this.getName() + ",生命值:" + this.getHealth()
                    + ",类型:" + this.getType());
    
        }
    
    }

    二、创建测试类(主类:角色介绍)

      方法的调用:在主方法创建角色实例,用实力对象调用方法

    /**
     * 游戏角色介绍测试类
     * 
     * @author Administrator
     *
     */
    public class Main {
    
        public static void main(String[] args) {
            // 创建角色实例
            Hero hero = new Hero();
            Weapons weap = new Weapons();
            Monster monster = new Monster();
            // 角色介绍
            hero.showInfo();
            weap.showInfo();
            monster.showInfo();
    
        }
    
    }

    三、测试修改Bug最终展示

      效果如下图:

     

  • 相关阅读:
    实验4-1-5 韩信点兵 (10分)
    实验4-1-6 求分数序列前N项和 (15分)
    实验7-1-5 选择法排序 (20分)
    实验7-1-2 求最大值及其下标 (20分)
    第一次个人编程作业
    3.Vue.js-目录结构
    2.VUEJS-安装
    1.Vuejs-第一个实例
    Mybatis通用Mapper介绍与使用
    商城项目团购之定时任务2
  • 原文地址:https://www.cnblogs.com/tengqiuyu/p/6911883.html
Copyright © 2020-2023  润新知