我们在看C#的代码的时候构造方法后面的冒号会跟this()和base(),网上对这个描述很散也不是很全,现在我在这边统一总结下,如有不对之处还请指出来。 this(),首先说明只能在自己类中使用,也就是说多个构造函数时,想调用同类中的另一个构造函数时就可以用this base(),是让子类来调用父类中构造方法的,这样就可以减少一些代码的书写 下面用代码将两个不同之处列出来 创建父类BaseMethodTest.cs 创建子类SubMethodTest.cs 测试类创建: 测试结果见下图 创建父类BaseMethodTest.cs 创建子类SubMethodTest.cs 测试类 TestMethod.cs 控制台的输出结果见下图,图中很明确的显示出实例化子类的时候先调用父类的构造函数然后才调用了子类的构造函数,这个就是方法冒号后调用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() { } }
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()演示说明
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() { } }
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() { } // 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() { } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算