How does classes Inheritance work in Unity?
Create an Empty Object and attach the scripts.
FruitSalad.cs
public class FruitSalad : MonoBehaviour // estende MonoBehaviour, è la classe dalla quale ereditano tutti i componenti dei nostri giochi { void Start() { //Let's illustrate inheritance with the //default constructors. Debug.Log("Creating the fruit"); Fruit myFruit = new Fruit(); Debug.Log("Creating the apple"); Apple myApple = new Apple(); //Call the methods of the Fruit class. //Now let's illustrate inheritance with the //constructors that read in a string. Debug.Log("Creating the fruit"); myFruit = new Fruit("yellow");// crea un'istanza della classe Fruit.cs Debug.Log("Creating the apple"); myApple = new Apple("green"); // crea un'istanza della classe Apple.cs //Call the methods of the Fruit class. myFruit.SayHello(); // scrive: Hello. I am a fruit. myFruit.Chop(); // scrive: The yellow fruit has been chopped //Call the methods of the Apple class. //Notice how class Apple has access to all //of the public methods of class Fruit. myApple.SayHello(); // scrive: Hello. I am a fruit. myApple.Chop(); // scrive: The green fruit has been chopped } } [/charp] Fruit.cs using UnityEngine; using System.Collections; //This is the base class which is //also known as the Parent class. public class Fruit { public string color; //This is the first constructor for the Fruit class //and is not inherited by any derived classes. public Fruit() // deve esserci obbligatoriamente il costruttore che riceve 0 parametri { color = "orange"; Debug.Log("1st Fruit Constructor Called"); } //This is the second constructor for the Fruit class //and is not inherited by any derived classes. public Fruit(string newColor) { color = newColor; Debug.Log("2nd Fruit Constructor Called"); } public void Chop() { Debug.Log("The " + color + " fruit has been chopped."); } public void SayHello() { Debug.Log("Hello, I am a fruit."); } }
Apple.cs
using UnityEngine; using System.Collections; //This is the derived class whis is //also know as the Child class. public class Apple : Fruit // Apple estende Fruit { //This is the first constructor for the Apple class. //It calls the parent constructor immediately, even //before it runs. public Apple() // deve esserci obbligatoriamente il costruttore che riceve 0 parametri { //Notice how Apple has access to the public variable //color, which is a part of the parent Fruit class. color = "red"; // la proprietà color è in Fruit.cs Debug.Log("1st Apple Constructor Called"); } //This is the second constructor for the Apple class. //It specifies which parent constructor will be called //using the "base" keyword. public Apple(string newColor) : base(newColor) { //Notice how this constructor doesn't set the color //since the base constructor sets the color that //is passed as an argument. Debug.Log("2nd Apple Constructor Called"); } }
On console:
– Creating the fruit
– 1st Fruit Constructor Called
– Creating the apple
– 1st Fruit Constructor Called
– 1st Apple Constructor Called
– Creating the fruit
– 2nd Fruit Constructor Called
– Creating the apple
– 2nd Fruit Constructor Called
– 2nd Apple Constructor Called
– Hello, I am a fruit
– The yellow fruit has been chopped
– Hello, I am a fruit
– The green fruit has been chopped
For italian people:
1. Fruit myFruit = new Fruit(); -> public Fruit(), il costruttore senza parametri
2. Apple myApple = new Apple(); -> public Fruit() poi public Apple()
3. myFruit = new Fruit(“yellow”); -> public Fruit(string newColor)
4. myApple = new Apple(“green”); -> -> public Fruit(string newColor) poi public Apple(string newColor) : base(newColor) specifica la chiamata al costruttore di Fruit.cs tramite la keyword : base(newColor) – estende newColor
5. Fruit.cs scrive Debug.Log(“Hello, I am a fruit.”); e Debug.Log(“The ” + color + ” fruit has been chopped.”);