iT邦幫忙

0

【C#學習筆記】12《類別(Class)》巢狀類別、主要建構子、this & base

  • 分享至 

  • xImage
  •  

【C#學習筆記】11《類別(Class)》抽象類別與抽象函式(abstract)、密封類別(sealed)
【C#學習筆記】13《命名規則速查表》


巢狀類別(Nested Class)

巢狀類別(Nested Class) 「是指把一個類別寫在另一個類別裡面」。
通常是因為「這個類別只跟外部類別有關,不想暴露給外面亂用」。
例如:SkillData只屬於SkillSystem使用。

public class SkillSystem
{
    public class SkillData
    {
    }
}

可以使整個架構分類清楚降低污染等。

遊戲化範例:
RPG遊戲為例,我們要創建多種職業的英雄,因此可以把最上層的Class設為Hero
每個英雄都會有自己的狀態或數值,而這些狀態或數值僅限於英雄能用,因此我們可以將其包入一個巢狀的class = State,未來英雄要擴充的所有數值都可以寫在裡面,方便管理。

public abstract class Hero
{ 
    public class Stats
    {
        private bool isAlive = true;
        private float hp;
        public float HP 
        { get
            {
                return hp;
            }
            set
            {
                if(value < 0)
                {
                    hp = 0;
                    isAlive = false;
                } 
                else hp = value;

            }
        }
        public float AttackPower { get; set; }
    }
}

主要建構子(Primary Constructor)

「主要建構子(Primary Constructor)」 是一種把建構子的參數直接寫在類別名稱後面的語法。
它的本質其實還是建構子,只是語法簡化。
一般建構子

public class Player
{
    private string name;
    private int hp;

    public Player(string name, int hp)
    {
        this.name = name;
        this.hp = hp;
    }
}

這是很常見的寫法,但相對來說有點冗長。
主要建構子

//name 和 hp 就是主要建構子的參數。
public class Player(string name, int hp)
{

}

建立物件:

Player p = new Player("Hero", 100);

繼承時怎麼用?

public class Player(string name)
{
    public string Name => name;
}

public class Warrior(string name, int hp)
    : Player(name)
{
    public int Hp => hp;
}

優點:
程式更短、適合不可變的資料及閱讀性高。
缺點:
容易誤會是欄位(Field),太複雜時反而難讀。

this & base

thisbase是C#很核心的兩個關鍵字

this

代表 「目前這個物件自己」
常用於以下目的:

區分成員變數與參數

public class Player
{
    private string name;

    public Player(string name)
    {
        this.name = name;
    }
}

這裡有兩個name:
參數name及成員變數namethis.name表示「這個物件裡面的 name」

呼叫自己的方法

public class Player
{
    public void Attack()
    {
        Console.WriteLine("攻擊");
    }

    public void Skill()
    {
        this.Attack();
    }
}

但其實C#會自動補this,所以大部分都不會寫出來。

呼叫自己的建構子

public class Player
{
    private string name;
    private int hp;

    public string Name { get; private set;  }
    public int HP { get; private set; }

    public Player(string name) : this(name, 100)
    {

    }

    public Player(string name, int hp)
    {
        this.Name = name;
        this.HP = hp;
    }
}
var Player = new Player("Alice");

Console.WriteLine($"Player Name: {Player.Name}, HP: {Player.HP}");
//Player Name: Alice, HP: 100

相當於執行new Player("Alice") == new Player("Alice",100)

base

base代表 「父類別(基底類別)」
常用於以下目的:

呼叫父類別建構子

public class Character
{
    public Character(string name)
    {
        Console.WriteLine(name);
    }
}

public class Warrior : Character
{
    public Warrior(string name)
        : base(name)//呼叫父類別的建構子
    {
    }
}

呼叫父類別的方法

public class Character
{
    public virtual void Attack()
    {
        Console.WriteLine("普通攻擊");
    }
}

public class Warrior : Character
{
    public override void Attack()
    {
        base.Attack();//執行父類別版本

        Console.WriteLine("劍氣");
    }
}

類別(Class)是物件導向的核心,而今天介紹的巢狀類別、主要建構子以及 thisbase 的運用,能讓的類別架構變得更有彈性且易讀。歡迎追蹤我的學習筆記系列,我們一起在C#的世界裡繼續點技能樹!


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言