using UnityEngine;
public class Inheritance : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Creating the fruit");
Fruit myFruit = new Fruit();
Debug.Log("Creating the apple");
Apple myApple = new Apple();
//调用 Fruit 类的方法。
myFruit.SayHello();
myFruit.Chop();
//调用 Apple 类的方法。
//注意 Apple 类如何访问
//Fruit 类的所有公共方法。
myApple.SayHello();
myApple.Chop();
//现在,让我们用读取字符串的
//构造函数来说明继承。
Debug.Log("Creating the yellow fruit");
myFruit = new Fruit("yellow");
Debug.Log("Creating the green apple");
myApple = new Apple("green");
//调用 Fruit 类的方法。
myFruit.SayHello();
myFruit.Chop();
//调用 Apple 类的方法。
//注意 Apple 类如何访问
//Fruit 类的所有公共方法。
myApple.SayHello();
myApple.Chop();
}
}
public class Fruit
{
public string color;
public Fruit()
{
color = "orange";
Debug.Log("1st Fruit Constructor Called");
}
public Fruit(string newColor)
{
this.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.");
}
}
public class Apple : Fruit
{
public Apple()
{
color = "red";
Debug.Log("1st Apple Constructor Called");
}
public Apple(string newColor):base(newColor)
{
Debug.Log("2nd Apple Constructor Called");
}
}