美文网首页
C# 构造函数 this base

C# 构造函数 this base

作者: 合肥黑 | 来源:发表于2022-01-24 09:07 被阅读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()的详细解释
  • this(),首先说明只能在自己类中使用,也就是说多个构造函数时,想调用同类中的另一个构造函数时就可以用this
  • base(),是让子类来调用父类中构造方法的,这样就可以减少一些代码的书写
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()
    {
        
    }
}

相关文章

  • C#构造函数中this和base的使用

    C#构造函数中this和base的使用 结论 构造函数的作用是,给对象成员进行初始化。 然后,构造函数this和b...

  • C# 构造函数 this base

    一、C#构造函数中:this()的作用[https://www.cnblogs.com/luguangguang/...

  • 2021-02-18【Dart】语法记录

    1.命名构造函数 c#改写:可以使用静态函数:

  • 面向对象(六)-派生类的构造函数

    派生类的构造函数 语法 如果不显式声明调用父类的无参构造函数(base()),那么默认会调用父类的无参构造函数。 ...

  • Unity面试刷题库

    C#问题 1.在类的构造函数前加上static会报什么错?为什么? 答:在构造函数如果有public修饰的静态构造...

  • C#构造函数

    什么是构造函数? 构造函数,是一种特殊的方法。主要用来在创建对象时初始化对象,即为对象成员变量赋初始值,总与new...

  • C# 构造函数

    每当创建类或结构时,将会调用其构造函数。 类或结构可能具有采用不同参数的多个构造函数。 使用构造函数,程序员能够设...

  • C#构造函数

    参考:https://docs.microsoft.com/zh-cn/dotnet/csharp/program...

  • 继承情况下的Constructor

    继承情况下的Constructor 在子类构造函数中调用基类构造函数时,obj=obj@Base();@前的obj...

  • 17、派生类的构造函数

    语法: tips: 如果不显式声明调用父类的无参构造函数(base()),那么默认会调用父类的无参构造函数。 无...

网友评论

      本文标题:C# 构造函数 this base

      本文链接:https://www.haomeiwen.com/subject/qszualtx.html