当方法名相同时,通过隐藏方法,派生类对象只能调用未隐藏的方法。
用法:通过new关键字修饰方法
个人理解:通过方法的隐藏,可以避免继承类同名方法产生二义性
**
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();}}

