C# 继承、重载、多态
写个测试,一看便知
父类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Component
{
public virtual void Test()
{
UnityEngine.Debug.Log("this is parent component");
}
}
子类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SubComponent : Component
{
public override void Test()
{
UnityEngine.Debug.Log("this is sub component");
}
}
调用结果:
//Component com = new SubComponent(); //com.Test();//print:this is sub component //Component com = new Component(); //com.Test();//print:this is parent component //Component com = new SubComponent(); //(com as SubComponent).Test();//print:this is sub component //Component com = new Component(); //(com as SubComponent).Test();//报错,print:NullReferenceException
博客园Jason_c微信打赏码
如果本篇文档对你有帮助,打赏Jason_c根华子吧,他的私房钱被老婆没收了,呜呜!
浙公网安备 33010602011771号