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!"); }}