C# 构造函数 this base

2022-01-24  本文已影响0人  合肥黑
一、C#构造函数中:this()的作用

通俗来说,可以说是构造函数的继承

1.继承无参时的构造函数
static void Main(string[] args)
        {
            AA aA = new AA("c","d");
            Console.WriteLine(aA.aa);
            Console.WriteLine(aA.bb);
            Console.WriteLine(aA.cc);
            Console.WriteLine(aA.dd);
            Console.WriteLine(aA.ee);

            Console.ReadKey();
        }
    }
    class AA
    {
      public  string aa="aa 未初始化";
      public  string bb= "bb 未初始化";
      public  string cc= "cc未初始化";
      public  string dd= "dd 未初始化";
      public  string ee= "ee 未初始化";
        public AA(string c,string d):this()
        {
            this.cc = c;
            this.dd = d;
        }
        public AA()
        {
            this.aa = "a";
            this.bb = "b";
        }
    }

new AA("c","d")调用了两个参数的构造函数,但是继承了无参的构造函数,显示结果如下


image.png
2.继承有参的构造函数
class Program
    {
        static void Main(string[] args)
        {
            AA aA = new AA("c","d");
            Console.WriteLine(aA.aa);
            Console.WriteLine(aA.bb);
            Console.WriteLine(aA.cc);
            Console.WriteLine(aA.dd);
            Console.WriteLine(aA.ee);

            Console.ReadKey();
        }
    }
    class AA
    {
      public  string aa="aa 未初始化";
      public  string bb= "bb 未初始化";
      public  string cc= "cc未初始化";
      public  string dd= "dd 未初始化";
      public  string ee= "ee 未初始化";
        public AA(string c,string d):this("e")  //此处初始化了一个参数
        {
            this.cc = c;
            this.dd = d;
        }
        public AA()
        {
            this.aa = "a";
            this.bb = "b";
        }
        //此处是新的带一个参数的构造函数
        public AA(string e)
        {
            this.ee = e;
        }
    }
image.png
二、C# 构造函数后面的冒号跟base()和this()的详细解释
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()
    {
        
    }
}
上一篇 下一篇

猜你喜欢

热点阅读