• 关于C#基类和子类函数调用问题


    c#基类子类的函数调用关系,代码说明newkeyword后面的类中的函数为对象调用的函数,当然必需要有virtual和override,继承就相当于包括了基类的函数,子类对象调用时基类的函数相当于就在子类其中一样。(必需要有virtual和override此代码才成立),问题是C#基础问题但非常easy搞错,代码片在unity3d中測试,需要UnityEngine.dll。

    using UnityEngine;
    using System.Collections;
    
    public class NewBehaviourScript : MonoBehaviour
    {
        void Start()
        {
            TestBase T1 = new TestChild();
            T1.TestA();//child!
    
            TestBase T2 = new TestBase();
            T2.TestA();//base!
    
            TestChild T3 = new TestChild();
            T1.TestA();//child!
    
        }
    
    }
    
    using UnityEngine;
    using System.Collections;
    using UnityEngine;
    public class TestBase
    {
        public TestBase()
        {
          
        }
        public virtual void A()
        {
            Debug.LogError("base!");
        }
    
        public void TestA()
        {
            A();
        }
    
    }
    public class TestChild : TestBase
    {
        public override void A()
        {
            Debug.LogError("child!");
        }
    }



  • 相关阅读:
    两数之和
    数组,链表,跳表
    第二讲:增长全景图
    三数之和
    第一讲:增长的本质
    移动零
    八十忆双亲+师友杂记
    java:从命令行接收多个数字,求和之后输出结果
    编程的精义读后感
    java语言基础第三讲作业
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3816384.html
Copyright © 2020-2023  润新知