继承

利用继承来重用代码并在相关类之间建立联系

Fruit 类

  1. //这是基类,也称为父类。
  2. public class Fruit
  3. {
  4. public string color;
  5. //这是 Fruit 类的第一个构造函数,不会被任何派生类继承。
  6. public Fruit()
  7. {
  8. color = "orange";
  9. Debug.Log("1st Fruit Constructor Called");
  10. }
  11. //这是 Fruit 类的第二个构造函数,不会被任何派生类继承。
  12. public Fruit(string newColor)
  13. {
  14. color = newColor;
  15. Debug.Log("2nd Fruit Constructor Called");
  16. }
  17. public void Chop()
  18. {
  19. Debug.Log("The " + color + " fruit has been chopped.");
  20. }
  21. public void SayHello()
  22. {
  23. Debug.Log("Hello, I am a fruit.");
  24. }
  25. }

Apple 类

  1. //这是派生类,也称为子类。
  2. public class Apple : Fruit
  3. {
  4. //这是 Apple 类的第一个构造函数。它立即调用父构造函数,甚至在它运行之前调用。
  5. public Apple()
  6. {
  7. //注意 Apple 如何访问公共变量 color,该变量是父 Fruit 类的一部分。
  8. color = "red";
  9. Debug.Log("1st Apple Constructor Called");
  10. }
  11. //这是 Apple 类的第二个构造函数。它使用“base”关键字指定要调用哪个父构造函数。
  12. public Apple(string newColor) : base(newColor)
  13. {
  14. //请注意,该构造函数不会设置 color,因为基构造函数会设置作为参数传递的 color。
  15. Debug.Log("2nd Apple Constructor Called");
  16. }
  17. }

FruitSalad 类

  1. public class FruitSalad : MonoBehaviour
  2. {
  3. void Start ()
  4. {
  5. //让我们用默认构造函数来说明继承。
  6. Debug.Log("Creating the fruit");
  7. Fruit myFruit = new Fruit();
  8. Debug.Log("Creating the apple");
  9. Apple myApple = new Apple();
  10. //调用 Fruit 类的方法。
  11. myFruit.SayHello();
  12. myFruit.Chop();
  13. //调用 Apple 类的方法。
  14. //注意 Apple 类如何访问Fruit 类的所有公共方法。
  15. myApple.SayHello();
  16. myApple.Chop();
  17. //现在,让我们用读取字符串的
  18. //构造函数来说明继承。
  19. Debug.Log("Creating the fruit");
  20. myFruit = new Fruit("yellow");
  21. Debug.Log("Creating the apple");
  22. myApple = new Apple("green");
  23. //调用 Fruit 类的方法。
  24. myFruit.SayHello();
  25. myFruit.Chop();
  26. //调用 Apple 类的方法。
  27. //注意 Apple 类如何访问
  28. //Fruit 类的所有公共方法。
  29. myApple.SayHello();
  30. myApple.Chop();
  31. }
  32. }

多态

使用多态、向上转换、向下转换在继承的类之间创建强大而动态的功能。

Fruit 类

  1. public class Fruit
  2. {
  3. public Fruit()
  4. {
  5. Debug.Log("1st Fruit Constructor Called");
  6. }
  7. public void Chop()
  8. {
  9. Debug.Log("The fruit has been chopped.");
  10. }
  11. public void SayHello()
  12. {
  13. Debug.Log("Hello, I am a fruit.");
  14. }
  15. }

Apple 类

  1. public class Apple : Fruit
  2. {
  3. public Apple()
  4. {
  5. Debug.Log("1st Apple Constructor Called");
  6. }
  7. //Apple 有自己的 Chop() 和 SayHello() 版本。
  8. //运行脚本时,请注意何时调用Fruit 版本的这些方法以及何时调用Apple 版本的这些方法。
  9. //此示例使用“new”关键字禁止来自 Unity 的警告,同时不覆盖Apple 类中的方法。
  10. public new void Chop()
  11. {
  12. Debug.Log("The apple has been chopped.");
  13. }
  14. public new void SayHello()
  15. {
  16. Debug.Log("Hello, I am an apple.");
  17. }
  18. }

FruitSalad 类

  1. public class FruitSalad : MonoBehaviour
  2. {
  3. void Start ()
  4. {
  5. //请注意,这里的变量“myFruit”的类型是Fruit,但是被分配了对 Apple 的引用。
  6. //这是由于多态而起作用的。由于 Apple 是 Fruit,因此这样是可行的。
  7. //虽然 Apple 引用存储在 Fruit 变量中,但只能像 Fruit 一样使用
  8. Fruit myFruit = new Apple();
  9. myFruit.SayHello();
  10. myFruit.Chop();
  11. //这称为向下转换。
  12. //Fruit 类型的变量“myFruit”实际上包含对 Apple 的引用。
  13. //因此,可以安全地将它转换回 Apple 变量。
  14. //这使得它可以像 Apple 一样使用,而在以前只能像 Fruit一样使用。
  15. Apple myApple = (Apple)myFruit;
  16. myApple.SayHello();
  17. myApple.Chop();
  18. }
  19. }