メンバーの隠蔽

Tutorial

intermediate

+0XP

5 mins

(12)

Unity Technologies

メンバーの隠蔽

派生クラスで基底クラスのメンバーの隠蔽を実装する方法を学びます。

Languages available:

1. メンバーの隠蔽

Humanoid クラスのコード

using UnityEngine;
using System.Collections;

public class Humanoid
{
    // Yell メソッドの基底クラスのバージョン
    public void Yell()
    {
        Debug.Log ("Humanoid version of the Yell() method");
    }
}

Enemy クラスのコード

using UnityEngine;
using System.Collections;

public class Enemy : Humanoid
{
    // この記述により、Humanoid クラスのバージョンが隠蔽されます。
    new public void Yell()
    {
        Debug.Log ("Enemy version of the Yell() method");
    }
}

Orc クラスのコード

using UnityEngine;
using System.Collections;

public class Orc : Enemy
{
    // この記述により、Enemy クラスのバージョンが隠蔽されます。
    new public void Yell()
    {
        Debug.Log ("Orc version of the Yell() method");
    }
}

WarBand クラスのコード

using UnityEngine;
using System.Collections;

public class WarBand : MonoBehaviour 
{
    void Start () 
    {
        Humanoid human = new Humanoid();
        Humanoid enemy = new Enemy();
        Humanoid orc = new Orc();

        // Humanoid 型の各変数に継承の階層内にある、
        // 異なるクラスのオブジェクトへの参照が格納されることに注意してください。
        // この場合、各オブジェクトから
        // Humanoid クラスの Yell() メソッドが呼ばれます。
        human.Yell();
        enemy.Yell();
        orc.Yell();
    }
}

Complete this Tutorial