物件導向是程式中非常重要的一環

如果你物件導向熟練  基本上C#的程式設計你就算是畢業了

所以再來我就開始講解物件導向到底是什麼

也許你聽過這名詞  但如果你不知道的話

接下來可能會稍微有點複雜

但物件導向算是非常有趣的東西

雖然物件導向有很多種方式

但我這邊不打算一次講完

那就讓我們先來看看繼承到底是什麼吧!!

繼承

public class Test : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        GameData cat = new GameData("Cat", 100, 50); //這裡
        Debug.Log(cat.GameName);
        Debug.Log(cat.Hp);
        Debug.Log(cat.Atk);
    }
}

// 遊戲底層
public class GameBase
{
    protected string    _GameName;  // 遊戲名稱
    protected int       _Hp;        // 你的血量
    protected int       _Atk;       // 你的攻擊
}

// 繼承 GameBase(遊戲底層) 
public class GameData : GameBase
{
    // 實體化傳入參數
    public GameData(string GameName, int Hp, int Atk)
    {
        this._GameName = GameName;
        this._Hp = Hp;
        this._Atk = Atk;
    }

    // 公開你的名稱 只能取
    public string GameName
    {
        get { return this._GameName; }
    }

    // 公開你的血量 只能取
    public int Hp
    {
        get { return this._Hp; }
    }

    // 公開你的攻擊 只能取
    public int Atk
    {
        get { return this._Atk; }
    }
}

我就稍微用之前教過的protecte(保護修飾詞)來做一個簡單的Demo

那主要繼承只是獲得繼承的目標所有東西

包括他的變數、功能等等

你一定又會問 那既成有什麼好處

你可以先看我程式中標記這裡的地方

在遊戲中怪物一定是有很多種

如果怪物的資料我們都獨立一個腳本去做的話是相當笨的事

我們只要把遊戲的底層資料給繼承

所有怪物的資料我們就只要做一個腳本資料就可以了

而在呼叫的時候也可以相當方便

範例

public class Test : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        // 用遊戲資料陣列
        GameData[] enemys = new GameData[] 
        {
            new GameData("Cat", 100, 50),   // 貓資料
            new GameData("Dog", 200, 65)    // 狗資料
        };

        // 用迴圈將資料印出來
        foreach(GameData enemy in enemys)
        {
            Debug.Log(string.Format("怪物名稱:{0} 血量:{1} 攻擊力:{2}", enemy.GameName, enemy.Hp, enemy.Atk));
        }
    }
}

在我的迴圈中

我只需要叫出GameData的資料型態就可以將他們的資料全部印出來

是不是相當方便

再來給大家看看沒有使用物件導向的笨方式

笨範例

將剛剛的遊戲底層改成貓

再複製一個class叫Dog的 (這裡就不貼出來了)

public class Cat
{
    protected string    _GameName;  // 遊戲名稱
    protected int       _Hp;        // 你的血量
    protected int       _Atk;       // 你的攻擊

    // 實體化傳入參數
    public Cat(string GameName, int Hp, int Atk)
    {
        this._GameName = GameName;
        this._Hp = Hp;
        this._Atk = Atk;
    }

    // 公開你的名稱 只能取
    public string GameName
    {
        get { return this._GameName; }
    }

    // 公開你的血量 只能取
    public int Hp
    {
        get { return this._Hp; }
    }

    // 公開你的攻擊 只能取
    public int Atk
    {
        get { return this._Atk; }
    }
}

使用上

public class Test : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        // 宣告貓跟狗的資料
        Cat cat = new Cat("Cat", 100, 50);
        Dog dog = new Dog("Dog", 200, 65);


        Debug.Log("名稱:" + cat.GameName + " 血量:" + cat.Hp + " 攻擊力:" + cat.Atk);
        Debug.Log("名稱:" + dog.GameName + " 血量:" + dog.Hp + " 攻擊力:" + dog.Atk);
    }
}

聰明的你應該知道笨在哪裡吧

如果我有100種怪物  我要建100個Class

而且在呼叫使用上也相當不方便  看上面我的Debug.Log中就知道了

要把所有有可能出現的怪物都打上去

這樣應該就明白物件導向-繼承的重要性了吧

如果還有不懂得  歡迎留言哦!!!

arrow
arrow
    全站熱搜

    Weight 發表在 痞客邦 留言(0) 人氣()