Overriding
Проверено с версией:: 4.1
-
Сложность: Средняя
How to override a base classes members with the members of a child class.


Overriding
Средняя Scripting
Fruit Class
Code snippet
using UnityEngine;
using System.Collections;
public class Fruit
{
public Fruit ()
{
Debug.Log("1st Fruit Constructor Called");
}
//These methods are virtual and thus can be overriden
//in child classes
public virtual void Chop ()
{
Debug.Log("The fruit has been chopped.");
}
public virtual void SayHello ()
{
Debug.Log("Hello, I am a fruit.");
}
}
#pragma strict
public class Fruit
{
public function Fruit ()
{
Debug.Log("1st Fruit Constructor Called");
}
//Overriding members happens automatically in
//Javascript and doesn't require additional keywords
public function Chop ()
{
Debug.Log("The fruit has been chopped.");
}
public function SayHello ()
{
Debug.Log("Hello, I am a fruit.");
}
}
import UnityEngine
import System.Collections
public class Fruit:
public def constructor():
Debug.Log('1st Fruit Constructor Called')
//These methods are virtual and thus can be overriden
//in child classes
public virtual def Chop():
Debug.Log('The fruit has been chopped.')
public virtual def SayHello():
Debug.Log('Hello, I am a fruit.')
Apple Class
Code snippet
using UnityEngine;
using System.Collections;
public class Apple : Fruit
{
public Apple ()
{
Debug.Log("1st Apple Constructor Called");
}
//These methods are overrides and therefore
//can override any virtual methods in the parent
//class.
public override void Chop ()
{
base.Chop();
Debug.Log("The apple has been chopped.");
}
public override void SayHello ()
{
base.SayHello();
Debug.Log("Hello, I am an apple.");
}
}
#pragma strict
public class Apple extends Fruit
{
public function Apple ()
{
Debug.Log("1st Apple Constructor Called");
}
//Overriding members happens automatically in
//Javascript and doesn't require additional keywords
public function Chop ()
{
super.Chop();
Debug.Log("The apple has been chopped.");
}
public function SayHello ()
{
super.SayHello();
Debug.Log("Hello, I am an apple.");
}
}
import UnityEngine
import System.Collections
public class Apple(Fruit):
public def constructor():
Debug.Log('1st Apple Constructor Called')
//These methods are overrides and therefore
//can override any virtual methods in the parent
//class.
public override def Chop():
super.Chop()
Debug.Log('The apple has been chopped.')
public override def SayHello():
super.SayHello()
Debug.Log('Hello, I am an apple.')
FruitSalad Class
Code snippet
using UnityEngine;
using System.Collections;
public class FruitSalad : MonoBehaviour
{
void Start ()
{
Apple myApple = new Apple();
//Notice that the Apple version of the methods
//override the fruit versions. Also notice that
//since the Apple versions call the Fruit version with
//the "base" keyword, both are called.
myApple.SayHello();
myApple.Chop();
//Overriding is also useful in a polymorphic situation.
//Since the methods of the Fruit class are "virtual" and
//the methods of the Apple class are "override", when we
//upcast an Apple into a Fruit, the Apple version of the
//Methods are used.
Fruit myFruit = new Apple();
myFruit.SayHello();
myFruit.Chop();
}
}
#pragma strict
function Start ()
{
var myApple = new Apple();
//Notice that the Apple version of the methods
//override the fruit versions. Also notice that
//since the Apple versions call the Fruit version with
//the "base" keyword, both are called.
myApple.SayHello();
myApple.Chop();
//Overriding is also useful in a polymorphic situation.
//Since the methods of the Fruit class are "virtual" and
//the methods of the Apple class are "override", when we
//upcast an Apple into a Fruit, the Apple version of the
//Methods are used.
var myFruit = new Apple();
myFruit.SayHello();
myFruit.Chop();
}
import UnityEngine
import System.Collections
public class FruitSalad(MonoBehaviour):
private def Start():
myApple = Apple()
//Notice that the Apple version of the methods
//override the fruit versions. Also notice that
//since the Apple versions call the Fruit version with
//the "base" keyword, both are called.
myApple.SayHello()
myApple.Chop()
//Overriding is also useful in a polymorphic situation.
//Since the methods of the Fruit class are "virtual" and
//the methods of the Apple class are "override", when we
//upcast an Apple into a Fruit, the Apple version of the
//Methods are used.
myFruit as Fruit = Apple()
myFruit.SayHello()
myFruit.Chop()
Связанные обучающие материалы
- Classes (Урок)
- Inheritance (Урок)
- Member Hiding (Урок)