C#面向對象之多態的幾個練習

一、計算平面圖形面積與周長

  • 定義抽象類Shape,類中有兩個抽象⽅法,分別是求⾯積Area和周⻓Circumference。
    定義圓類Circle,繼承⾃Shape,類中有成員變量radius表示半徑,實現Shape中求周⻓和⾯積的⽅法
    定義矩形類Rectangle,繼承⾃Shape,類中有成員變量length,width表示⻓和寬,
    實現Shape中的求周⻓和⾯積的⽅法
    定義靜態⽅法求⼀個圖形的⾯積和周⻓,傳⼊不同的參數,查看計算結果

代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shape
{
    #region 第一題

    /// <summary>
    /// 圖形類
    /// </summary>
    abstract class Shape
    {
        /// <summary>
        /// 求面積
        /// </summary>
        /// <returns></returns>
        public abstract double Area();

        /// <summary>
        /// 求周長
        /// </summary>
        /// <returns></returns>
        public abstract double Circumference();

    }

    /// <summary>
    /// 圓類
    /// </summary>
    class Circle : Shape
    {
        /// <summary>
        /// 半徑
        /// </summary>
        public double radius;

        /// <summary>
        /// 重寫圖形類的求面積方法
        /// </summary>
        /// <returns></returns>
        public override double Area()
        {
            return Math.Round(Math.PI * radius * radius, 2);
        }

        /// <summary>
        /// 重寫圖形類中的求周長方法
        /// </summary>
        /// <returns></returns>
        public override double Circumference()
        {
            return Math.Round(Math.PI * 2d * radius, 2);
        }


    }

    /// <summary>
    /// 三角形類,繼承圖形類
    /// </summary>
    class rectangle : Shape
    {
        /// <summary>
        /// 長
        /// </summary>
        public double length;

        /// <summary>
        /// 寬
        /// </summary>
        public double width;

        /// <summary>
        /// 重寫圖形的求面積方法
        /// </summary>
        /// <returns></returns>
        public override double Area()
        {
            return Math.Round(length * width, 2);
        }
        
        /// <summary>
        /// 重寫圖形類中的額求周長方法
        /// </summary>
        /// <returns></returns>
        public override double Circumference()
        {
            return Math.Round(2 * (length + width), 2);
        }
    }


    #endregion


    class Program
    {
        /// <summary>
        /// 靜態方法,求圖形面積和周長並打印
        /// </summary>
        /// <param name="shape"></param>
        public static void Calculation(Shape shape)
        {
            Console.WriteLine("該圖形的面積是:"+shape.Area());
            Console.WriteLine("該圖形的周長是:" + shape.Circumference());
        }
        static void Main(string[] args)
        {
            Circle circle = new Circle();
            circle.radius = 3;
            Calculation(circle);
        }
    }
}

二、模擬植物⼤戰僵⼫

  • 定義僵⼫類:
    公共成員變量:總⾎量
    ⽅法:受傷掉⾎(抽象⽅法)、死亡(抽象⽅法)
  • 定義普通僵⼫類繼承於僵⼫類
  • 定義有防具(路障)僵⼫類繼承於僵⼫類:
    特有成員變量:是否有防具
    特有⽅法:防具被打爛
  • 在Main⽅法⾥⾯⾥⽤循環語句模擬攻擊:
    兩隻個僵⼫對象同時被攻擊
    普通僵⼫被打擊時:每次掉⾎3.
    路障僵⼫被打擊時:有路障時,每次失⾎2,⾎量剩餘⼀半時,防具被打攔,之後
    每次失⾎3.
    循環攻擊過程中:每個僵⼫被攻擊時,輸出本次丟失⾎量,剩餘⾎量。失去道具
    時,輸出丟失的道具,僵⼫死亡時,輸出已死亡。
    最後⼀個僵⼫死亡時,攻擊停⽌,循環結束,輸出總攻擊次數

代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cPlantsVSZombies
{
    #region 第二題

    abstract class Zombies
    {
        /// <summary>
        /// 血量
        /// </summary>
        public int hp;

        /// <summary>
        /// 是否死亡,默認否
        /// </summary>
        public bool isDead = false;

        /// <summary>
        /// 構造方法給血量賦值
        /// </summary>
        /// <param name="hp"></param>
        public Zombies(int hp)
        {
            this.hp = hp;
        }

        /// <summary>
        /// 受傷減血方法
        /// </summary>
        public abstract void UnderAttack();

        /// <summary>
        /// 死亡方法
        /// </summary>
        public abstract void Dead();
    }

    /// <summary>
    /// 經典殭屍
    /// </summary>
    class NormalZombies : Zombies
    {
        public NormalZombies(int hp):base(hp)
        {
        }

        /// <summary>
        /// 重寫殭屍類的死亡方法
        /// </summary>
        public override void Dead()
        {
            isDead = true;
            Console.WriteLine("殭屍已死亡!");
        }

        /// <summary>
        /// 重寫殭屍類的受傷方法
        /// </summary>
        public override void UnderAttack()
        {
            if (isDead)
            {
                return;
            }
            hp -= 3;

            Console.WriteLine($"普通殭屍收到攻擊。本次丟失血量3,剩餘血量{hp}。");

            if (hp <= 0)
            {
                Dead();
                return;
            }
        }
    }

    /// <summary>
    /// 路障殭屍
    /// </summary>
    class RoadblockZombies : Zombies
    {
        /// <summary>
        /// 防具是否持有,默認持有
        /// </summary>
        public bool havaRoadblock = true;

        /// <summary>
        /// 半血血量
        /// </summary>
        public int halfHp;

        public RoadblockZombies(int hp) : base(hp)
        {
            halfHp = hp / 2;
        }

        /// <summary>
        /// 重寫殭屍類的死亡方法
        /// </summary>
        public override void Dead()
        {
            isDead = true;
            Console.WriteLine("殭屍已死亡!");
        }

        /// <summary>
        /// 重寫殭屍類的受傷方法
        /// </summary>
        public override void UnderAttack()
        {
            if (isDead)
            {
                return;
            }

            if (havaRoadblock)
            {
                hp -= 2;
                Console.WriteLine($"路障殭屍收到攻擊。本次丟失血量2,剩餘血量{hp}。");
            }
            else
            {
                hp -= 3;
                Console.WriteLine($"路障殭屍收到攻擊。本次丟失血量3,剩餘血量{hp}。");
            }


            if (hp <= halfHp)
            {
                LoseArmor();
            }

            if (hp <= 0)
            {
                Dead();
                return;
            }
        }

        /// <summary>
        /// 丟失防具方法
        /// </summary>
        public void LoseArmor()
        {
            havaRoadblock = false;
            Console.WriteLine("路障殭屍丟失了防具!");
        }
    }




    #endregion

    class Program
    {
        static void Main(string[] args)
        {
            NormalZombies normalZombies = new NormalZombies(100);
            RoadblockZombies roadblockZombies = new RoadblockZombies(100);

            int attackAcount = 0;

            while (true)
            {
                normalZombies.UnderAttack();
                roadblockZombies.UnderAttack();
                if (!normalZombies.isDead && !roadblockZombies.isDead)
                {
                    attackAcount += 2;
                }
                else if (normalZombies.isDead && roadblockZombies.isDead)
                {
                    break;
                }
                else
                {
                    attackAcount++;
                }
            }

            Console.WriteLine($"共計攻擊了{attackAcount}次!");

        }
    }
}

三、 模擬裝備商店

  • 定義靜態類GoodStore表示物品商店。
    物品商店中有銷售消耗品Comsumabel,裝備類Equipment,武器Weapon,這三種物品都繼承於抽象類物品類Goods。
  • 靜態類GoodStore中有三個貨櫃(數組)分別出售消耗品,裝備,武器。
    在靜態構造函數當中初始化這三個貨櫃,定義⽅法,當主⻆想買某種類型的物品,商店能夠全部打印出來進⾏介紹。
  • 抽象類Good:
  • 字段:name(string),type(包括消耗品,裝備,武器,可以使⽤枚舉來表示),buyPrice(買⼊價格,int),salePrice(賣出價格,int)
    抽象⽅法:Description(⽤於每類物品的詳細說明)
  • 消耗品:
    特有字段:hpAdd(int),mpAdd(int)
  • 裝備類:
    特有字段:propertyAdd(int)
  • 武器類:
    特有字段:attack(攻擊⼒,int)

代碼如下:

using System;

namespace dEquipmentStroe
{
    /// <summary>
    /// 物品商店
    /// </summary>
    static class GoodStore
    {

        static public Comsumabel[] comsumabels;
        static public Equipment[] equipments;
        static public Weapon[] weapons;

        static GoodStore()
        {
            comsumabels = new Comsumabel[5];
            equipments = new Equipment[5];
            weapons = new Weapon[5];
        }

        /// <summary>
        /// 打印商品詳細信息
        /// </summary>
        /// <param name="good"></param>
        public static void PrintIntroduce(Good[] goods)
        {
            if (goods != null)
            {
                foreach (var item in goods)
                {
                    if (item != null)
                    {
                        item.Description();
                    }
                }
            }
        }


    }

    /// <summary>
    /// 商品類型
    /// </summary>
    enum GoodType
    {
        /// <summary>
        /// 消耗品
        /// </summary>
        Comsumabel,

        /// <summary>
        /// 裝備
        /// </summary>
        Equipment,

        /// <summary>
        /// 武器
        /// </summary>
        Weapon
    }

    /// <summary>
    /// 物品類
    /// </summary>
    abstract class Good
    {
        /// <summary>
        /// 商品名
        /// </summary>
        public string name;

        /// <summary>
        /// 商品類型
        /// </summary>
        public GoodType type;

        /// <summary>
        /// 商品售價
        /// </summary>
        public int buyPrice;

        /// <summary>
        /// 商品回收價
        /// </summary>
        public int sellPrice;

        public Good(string name, GoodType type, int buyPrice, int sellPrice)
        {
            this.name = name;
            this.type = type;
            this.buyPrice = buyPrice;
            this.sellPrice = sellPrice;
        }

        /// <summary>
        /// 商品描述
        /// </summary>
        public abstract void Description();
    }

    /// <summary>
    /// 消耗品類,繼承物品類
    /// </summary>
    class Comsumabel : Good
    {
        /// <summary>
        /// 生命回覆
        /// </summary>
        public int hpAdd;

        /// <summary>
        /// 魔法回覆
        /// </summary>
        public int mpAdd;

        public Comsumabel(string name, GoodType type, int buyPrice, int sellPrice, int hpAdd, int mpAdd):base(name, type,buyPrice,sellPrice)
        {
            this.hpAdd = hpAdd;
            this.mpAdd = mpAdd;
        }

        public override void Description()
        {
            Console.WriteLine($"商品名:{name}|類型:{type}|售價{buyPrice}|回收價{sellPrice};使用效果:生命回覆{hpAdd},魔法回覆{mpAdd}。");
        }
    }

    /// <summary>
    /// 裝備類
    /// </summary>
    class Equipment : Good
    {
        /// <summary>
        /// 屬性增加
        /// </summary>
        public int propertyAdd;
        public Equipment(string name, GoodType type, int buyPrice, int sellPrice, int propertyAdd) : base(name, type, buyPrice, sellPrice)
        {
            this.propertyAdd = propertyAdd;
        }

        public override void Description()
        {
            Console.WriteLine($"商品名:{name}|類型:{type}|售價{buyPrice}|回收價{sellPrice};屬性增加:{propertyAdd}。");
        }

    }

    /// <summary>
    /// 武器類
    /// </summary>
    class Weapon : Good
    {
        /// <summary>
        /// 攻擊力
        /// </summary>
        public int attack;

        public Weapon(string name, GoodType type, int buyPrice, int sellPrice, int attack) : base(name, type, buyPrice, sellPrice)
        {
            this.attack = attack;
        }

        public override void Description()
        {
            Console.WriteLine($"商品名:{name}|類型:{type}|售價{buyPrice}|回收價{sellPrice};攻擊力:{attack}。");
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Comsumabel comsumabel01 = new Comsumabel("小紅", GoodType.Comsumabel, 100, 50, 100, 0);
            Comsumabel comsumabel02 = new Comsumabel("中紅", GoodType.Comsumabel, 150, 75, 200, 0);
            Comsumabel comsumabel03 = new Comsumabel("大紅", GoodType.Comsumabel, 200, 100, 300, 50);

            Equipment equipment01 = new Equipment("小面具", GoodType.Equipment, 900, 450, 20);
            Equipment equipment02 = new Equipment("中面具", GoodType.Equipment, 1300, 650, 30);
            Equipment equipment03 = new Equipment("大面具", GoodType.Equipment, 2000, 1000, 60);

            Weapon weapon01 = new Weapon("小劍", GoodType.Weapon, 500, 250, 10);
            Weapon weapon02 = new Weapon("中劍", GoodType.Weapon, 1000, 500, 30);
            Weapon weapon03 = new Weapon("大劍", GoodType.Weapon, 2000, 1000, 80);

            GoodStore.comsumabels[0] = comsumabel01;
            GoodStore.comsumabels[1] = comsumabel02;
            GoodStore.comsumabels[2] = comsumabel03;

            GoodStore.equipments[0] = equipment01;
            GoodStore.equipments[1] = equipment02;
            GoodStore.equipments[2] = equipment03;

            GoodStore.weapons[0] = weapon01;
            GoodStore.weapons[1] = weapon02;
            GoodStore.weapons[2] = weapon03;

            GoodStore.PrintIntroduce(GoodStore.comsumabels);


        }
    }
}

以上主要考察的是對C#中面向對象的多態的掌握

有疑問的可以私信博主。
點個關注,給個讚唄!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章