自己从0开始学习Unity的笔记 VIII (C#中类继承练习 II)

自己写了一个关于兵种的,因为一直在测试,到底面向对象是个什么玩意。。。然后就做了这个

namespace 兵种
{
    class Role //作为父类,构建一个普通角色属性用于继承
    {
        protected int health;
        protected int attack;
        protected int speed;
        protected int defend;

        public Role(int health,int attack,int speed,int defend)
        {
            this.health = health;
            this.attack = attack;
            this.speed = speed;
            this.defend = defend;
        }

        public void RoleData()
        {
            Console.WriteLine("你的人物属性是");
            Console.WriteLine("HP   :"+health);
            Console.WriteLine("攻击 :"+attack);
            Console.WriteLine("防御 :"+defend);
            Console.WriteLine("速度 :"+speed);
        }

     }

    class Saber : Role   //继承父类,创建一个剑士角色
    {
        private string position = "剑士";
        public Saber(int health, int attack, int speed, int defend) : base(health, attack, speed, defend)
        {
        }
        public void skill()
        {
            Console.WriteLine("职业 :"+position);
            Console.WriteLine("必杀技:EX咖喱棒");
        }
    }

    class Archer : Role  //继承父类,创建一个弓箭角色
    {
        private string position = "弓箭手";
        public Archer(int health, int attack, int speed, int defend) : base(health, attack, speed, defend)
        {
        }
        public void skill()
        {
            Console.WriteLine("职业 :" + position);
            Console.WriteLine("必杀技:无限剑制");
        }
    }

    class Caster : Role   //继承父类,创建一个法师角色
    {
        private string position = "魔法师";
        public Caster(int health, int attack, int speed, int defend) : base(health, attack, speed, defend)
        {
        }
        public void skill()
        {
            Console.WriteLine("职业 :" + position);
            Console.WriteLine("必杀技:理想乡");
        }
    }


    class RoleDataPosition     //这是一次尝试,把职业数据写在一个类里面,这样不在主函数里面也方便查找和修改。
    {
        public Saber dataSaber = new Saber(120, 60, 50, 50);
        public Archer dataArcher = new Archer(110, 70, 70, 40);
        public Caster dataCaster = new Caster(100, 80, 60, 30);

       
    }

    class Program
    {
        static void Main(string[] args)
        {
            RoleDataPosition playerData = new RoleDataPosition();
            for (int i = 0; i < 1; i++)  //本来想用while,想了一下还是算了
            {
                Console.WriteLine("请选择你想要的职业");
                Console.WriteLine("1.剑士");
                Console.WriteLine("2.弓箭手");
                Console.WriteLine("3.魔法师");
                char choose = Console.ReadKey(true).KeyChar;
                switch (choose)
                {
                    case '1':
                        playerData.dataSaber.RoleData();
                        playerData.dataSaber.skill();
                        break;
                    case '2':
                        playerData.dataArcher.RoleData();
                        playerData.dataArcher.skill();
                        break;
                    case '3':
                        playerData.dataCaster.RoleData();
                        playerData.dataCaster.skill();
                        break;
                    default:
                        Console.Clear();       
                        i--;                          //用于输入错误不让跳出用
                        break;
                }
            }


            Console.ReadKey();

        }
    }
}

记录这个的原因,是因为我觉得,如果以后怪兽多了,也可以建立这样一个类,存储所有怪兽的属性,大概可能有这样的一个扩展空间,方便以后自己查询,先记录一下

上一篇:Spring Cloud health节点通过注册中心扫描状态的简单实现


下一篇:ChengDu University Mental Health Website