我们在看C#的代码的时候构造方法后面的冒号会跟this()和base(),网上对这个描述很散也不是很全,现在我在这边统一总结下,如有不对之处还请指出来。
this(),首先说明只能在自己类中使用,也就是说多个构造函数时,想调用同类中的另一个构造函数时就可以用this
base(),是让子类来调用父类中构造方法的,这样就可以减少一些代码的书写
下面用代码将两个不同之处列出来
构造方法冒号后跟 this() 演示说明
创建父类BaseMethodTest.cs
using UnityEngine;
public class BaseMethodTest : MonoBehaviour
{
//没有参数的父类构造函数
public BaseMethodTest() {
Debug.Log("BaseMethodTest has no parameter");
}
//一个参数的父类构造函数
public BaseMethodTest(int a)
{
Debug.Log("BaseMethodTest has one parameter a="+a);
}
//两个参数的父类构造函数
public BaseMethodTest(int a,string str)
{
Debug.Log("BaseMethodTest has two parameters a=" + a+" str=="+str);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
创建子类SubMethodTest.cs
using UnityEngine;
public class SubMethodTest : BaseMethodTest
{
//子类没有参数的构造参数,继承了父类中没有参数的构造函数
public SubMethodTest() : base(){
Debug.Log("==== SubMethodTest has no parameter");
}
//子类有一个参数的构造参数,继承了父类中有一个参数的构造函数
public SubMethodTest(int a) : this()
{
Debug.Log("==== SubMethodTest has one parameter a=="+a);
}
//子类有两个参数的构造参数,继承了父类中有两个参数的构造函数
public SubMethodTest(int a,string s) : this(a)
{
Debug.Log("==== SubMethodTest has two parameter a==" + a+" s="+s);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
测试类创建:
using UnityEngine;
public class TestMethod : MonoBehaviour
{
public SubMethodTest subMethodNo;
public SubMethodTest subMethodOne;
public SubMethodTest subMethodTwo;
// Start is called before the first frame update
void Start()
{
//调用没有参数的子类构造方法,实例化子类对象
subMethodNo = new SubMethodTest();
//调用有一个参数的子类构造方法,实例化子类对象
subMethodOne = new SubMethodTest(10);
//调用有两个参数的子类构造方法,实例化子类对象
subMethodTwo = new SubMethodTest(10,"Hello Method");
}
// Update is called once per frame
void Update()
{
}
}
测试结果见下图
构造方法冒号后跟 base()演示说明
创建父类BaseMethodTest.cs
using UnityEngine;
public class BaseMethodTest : MonoBehaviour
{
//没有参数的父类构造函数
public BaseMethodTest() {
Debug.Log("BaseMethodTest has no parameter");
}
//一个参数的父类构造函数
public BaseMethodTest(int a)
{
Debug.Log("BaseMethodTest has one parameter a="+a);
}
//两个参数的父类构造函数
public BaseMethodTest(int a,string str)
{
Debug.Log("BaseMethodTest has two parameters a=" + a+" str=="+str);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
//写一个方法子类可以直接使用base.方法名()就可以调用该方法了
public int add() {
return 10 + 20;
}
}
创建子类SubMethodTest.cs
using UnityEngine;
public class SubMethodTest : BaseMethodTest
{
//子类没有参数的构造参数,继承了父类中没有参数的构造函数
public SubMethodTest() : base(){
Debug.Log("==== SubMethodTest has no parameter");
}
//子类有一个参数的构造参数,继承了父类中有一个参数的构造函数
public SubMethodTest(int a) : base(a)
{
Debug.Log("==== SubMethodTest has one parameter a=="+a);
}
//子类有两个参数的构造参数,继承了父类中有两个参数的构造函数
public SubMethodTest(int a,string s) : base(a,s)
{
Debug.Log("==== SubMethodTest has two parameter a==" + a+" s="+s);
}
// Start is called before the first frame update
void Start()
{
//测试使用base直接调用父类中的方法
Debug.Log("add====="+base.add());
}
// Update is called once per frame
void Update()
{
}
}
测试类 TestMethod.cs
using UnityEngine;
public class TestMethod : MonoBehaviour
{
public SubMethodTest subMethodNo;
public SubMethodTest subMethodOne;
public SubMethodTest subMethodTwo;
// Start is called before the first frame update
void Start()
{
//调用没有参数的子类构造方法,实例化子类对象
subMethodNo = new SubMethodTest();
//调用有一个参数的子类构造方法,实例化子类对象
subMethodOne = new SubMethodTest(10);
//调用有两个参数的子类构造方法,实例化子类对象
subMethodTwo = new SubMethodTest(10,"Hello Method");
}
// Update is called once per frame
void Update()
{
}
}
控制台的输出结果见下图,图中很明确的显示出实例化子类的时候先调用父类的构造函数然后才调用了子类的构造函数,这个就是方法冒号后调用base() 的作用,希望大家可以清晰的看出这个作用。