zoukankan      html  css  js  c++  java
  • 多态

    如何使用多态 (Polymorphism)、向上转换 (Upcasting) 和向下转换 (Downcasting) 在继承的类之间创建强大而动态的功能。

    Fruit 类

    using UnityEngine;
    using System.Collections;
    
    public class Fruit 
    {
        public Fruit()
        {
            Debug.Log("1st Fruit Constructor Called");
        }
    
        public void Chop()
        {
            Debug.Log("The fruit has been chopped.");        
        }
    
        public void SayHello()
        {
            Debug.Log("Hello, I am a fruit.");
        }
    }

    Apple 类

    using UnityEngine;
    using System.Collections;
    
    public class Apple : Fruit 
    {
        public Apple()
        {
            Debug.Log("1st Apple Constructor Called");
        }
    
        //Apple 有自己的 Chop() 和 SayHello() 版本。
        //运行脚本时,请注意何时调用
        //Fruit 版本的这些方法以及何时调用
        //Apple 版本的这些方法。
        //此示例使用“new”关键字禁止
        //来自 Unity 的警告,同时不覆盖
        //Apple 类中的方法。
        public new void Chop()
        {
            Debug.Log("The apple has been chopped.");        
        }
    
        public new void SayHello()
        {
            Debug.Log("Hello, I am an apple.");
        }
    }

    FruitSalad 类

    using UnityEngine;
    using System.Collections;
    
    public class FruitSalad : MonoBehaviour
    {
        void Start () 
        {
            //请注意,这里的变量“myFruit”的类型是
            //Fruit,但是被分配了对 Apple 的引用。这是
            //由于多态而起作用的。由于 Apple 是 Fruit,
            //因此这样是可行的。虽然 Apple 引用存储
            //在 Fruit 变量中,但只能像 Fruit 一样使用
            Fruit myFruit = new Apple();
    
            myFruit.SayHello();
            myFruit.Chop();
    
            //这称为向下转换。Fruit 类型的变量“myFruit”
            //实际上包含对 Apple 的引用。因此,
            //可以安全地将它转换回 Apple 变量。这使得
            //它可以像 Apple 一样使用,而在以前只能像 Fruit
            //一样使用。
            Apple myApple = (Apple)myFruit;
    
            myApple.SayHello();
            myApple.Chop();    
        }
    }
  • 相关阅读:
    数据结构之队列
    数据结构之循环链表-c语言实现
    数据结构之栈-c语言实现
    数据结构之栈
    vue v-model原理实现
    vue中使用mixins
    async和await
    vue组件中使用watch响应数据
    vue组件中使用<transition></transition>标签过渡动画
    react-motion 动画案例介绍
  • 原文地址:https://www.cnblogs.com/Mr-Prince/p/14142077.html
Copyright © 2011-2022 走看看