Create an Empty Object abd attach this scripts:
– WarBand.cs
using UnityEngine; using System.Collections; public class WarBand : MonoBehaviour // banda da guerra { void Start() { Humanoid human = new Humanoid(); // istanzio Humanoid enemy = new Enemy(); Humanoid orc = new Orc(); //Notice how each Humanoid variable contains //a reference to a different class in the //inheritance hierarchy, yet each of them //calls the Humanoid Yell() method. human.Yell(); // scrive Humanoid version of the Yell() method enemy.Yell(); // scrive Humanoid version of the Yell() method orc.Yell(); // scrive Humanoid version of the Yell() method } }
– Humanoid.cs
using UnityEngine; using System.Collections; public class Humanoid { //Base version of the Yell method public void Yell() // urlo { Debug.Log("Humanoid version of the Yell() method"); } }
– Enemy.cs
using UnityEngine; using System.Collections; public class Enemy : Humanoid { //This hides the Humanoid version. new public void Yell() { Debug.Log("Enemy version of the Yell() method"); } }
– Orc.cs
using UnityEngine; using System.Collections; public class Orc : Enemy { //This hides the Enemy version. new public void Yell() { Debug.Log("Orc version of the Yell() method"); } }
Console:
Humanoid version of the Yell() method
Humanoid version of the Yell() method
Humanoid version of the Yell() method
For italian people, come funziona?
Abbiamo una ereditarietà tra Humanoids -> Enemy -> Orcs e tre metodi con lo stesso nome
Dichiarando la funzione con la keyword – new – nascondiamo i metodi – new – in favore del metodo del padre – Humanoid.cs – che prende il sopravvento.