C#面向对象案例之奥特曼打小怪兽_02(奥特曼可挂载装备)

本文通过C#面向对象编程,详细介绍了如何构建一个奥特曼角色,该角色能够挂载不同装备来提升能力,以此为例阐述了面向对象设计的基本思想和实现方法。
摘要由CSDN通过智能技术生成
 class Monster
    {
   
        public string name;
        public int hp;
        public int atk;

        public Monster(string name, int atk, int hp)
        {
   
            this.hp = hp;
            this.name = name;
            this.atk = atk;
        }
        /// <summary>
        /// 怪的攻击方法
        /// 攻击outman
        /// </summary>
        public virtual void Attack(OutMan outMan)
        {
   
            //:调用OutMan的受伤方法
            outMan.Damager(atk);
            Console.WriteLine("{0}对{1},发起攻击,{2}血量为:{3}", name, outMan.name, outMan.name, outMan.hp);
        }

        /// <summary>
        /// 默认受伤方法
        /// </summary>
        /// <param name="atk"></param>
        public virtual void Damager(int atk)
        {
   
            hp -= atk;
            Console.WriteLine("{0}受伤了,血量为:{1}", name, hp);
        }
    }
    /// <summary>
    /// 哥布林类攻击方法就是父类中的默认攻击方法就够了,那么久不需要重写
    /// </summary>
    class Goblin : Monster
    {
   
        public int def;
        public Goblin(string name, int atk, int hp, int def) : base(name, atk, hp)
        {
   
            this.def = def;
        }

        public override void Damager(int atk)
        {
   
            if (atk - def > 0)
            {
   
                hp = hp - (atk - def);
                Console.WriteLine("{0}受伤了,血量为:{1}", name, hp);
            }
            else
            {
   
                Console.WriteLine("怪{0}闪避", name);
            }
        }
    }

    /// <summary>
    /// 哥斯拉这个类攻击方法和父类不一样:有概率翻倍
    /// 但是没有防御力,受伤方法可以直接用父类中的
    /// </summary>
    class Gesla : Monster
    {
   
        Random r;
        public Gesla(string name, int atk
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LilyCoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值