如果一个类继承另一个类,那么继承的那个类叫它的父类。
如果父类有自己的构造方法,子类也有,如果是父类 new 父类,那么调用的是父类的构造方法。
如果父类有自己的构造方法,子类也有,如果是子类 new 子类,那么先调用的是父类的构造方法,然后在调用子类的构造方法。
如果父类有自己的构造方法,子类也有,如果是父类 new 子类,那么先调用的是父类的构造方法,然后在调用子类的构造方法。
如果一个子类重写父类的方法,如果是父类 new 父类,那么调用的是父类的方法。
如果一个子类重写父类的方法,如果是子类 new 子类,那么调用的是子类的方法。
如果一个子类重写父类的方法,如果是父类 new 子类,那么调用的是子类的方法。
父类代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//父类
public class BaseUI
{
public BaseUI() {
Debug.Log("我是父类构造方法");
}
public virtual void BaseUI2() {
Debug.Log("重写父类方法1");
}
}
子类代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//子类
public class BeiBao_Panel : BaseUI
{
public BeiBao_Panel()
{
Debug.Log("我是子类构造方法");
}
public override void BaseUI2()
{
Debug.Log("重写父类方法2");
}
}
输出类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameRoot : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("----------b1---------");
BaseUI b1 = new BaseUI();
b1.BaseUI2();
Debug.Log("----------bb1--------");
BeiBao_Panel bb1 = new BeiBao_Panel();
bb1.BaseUI2();
Debug.Log("----------b2---------");
BaseUI b2 = new BeiBao_Panel();
b2.BaseUI2();
}
// Update is called once per frame
void Update()
{
}
}
输出结果: