【C#】构造函数后面的冒号跟base()和this()
c# this 构造函数 base 后面 冒号
2023-09-11 14:14:50 时间
this(),首先说明只能在自己类中使用,也就是说多个构造函数时,想调用同一个类中的另一个构造函数时就可以用this
base(),是让子类来调用父类中构造方法的,这样就可以减少一些代码的书写
注:
1.子类没有继承父类的构造函数
2.但子类默认会调用父类无参数的构造函数。调用父类无参构造函数是为了创建父类对象,让子类可以使用父类中的成员。
构造方法冒号后跟 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() 的作用,希望大家可以清晰的看出这个作用。
相关文章
- CefSharp F12打开DevTools查看console js和c#方法互相调用
- C# Regex类用法
- C#中this在构造函数时的使用
- c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习
- C#7.2——编写安全高效的C#代码 c# 中模拟一个模式匹配及匹配值抽取 走进 LINQ 的世界 移除Excel工作表密码保护小工具含C#源代码 腾讯QQ会员中心g_tk32算法【C#版】
- c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)
- EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- 利用反射快速给Model实体赋值 使用 Task 简化异步编程 Guid ToString 格式知多少?(GUID 格式) Parallel Programming-实现并行操作的流水线(生产者、消费者) c# 无损高质量压缩图片代码 8种主要排序算法的C#实现 (一) 8种主要排序算法的C#实现 (二)
- c# 把一个匿名对象赋值给一个Object类型的变量后,怎么取这个变量? c# dynamic动态类型和匿名类 详解C# 匿名对象(匿名类型)、var、动态类型 dynamic 深入浅析C#中的var和dynamic
- C#中泛型方法与泛型接口 C#泛型接口 List<IAll> arssr = new List<IAll>(); interface IPerson<T> c# List<接口>小技巧 泛型接口协变逆变的几个问题
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- C#MessageBox.Show报错,无法识别函数
- C#学习记录——循环
- 【C#】T this[string/int key]{ get; set; }索引器
- C#学习笔记(1)
- C#串口通信—向串口发送数据,同步接收返回数据
- C#中Socket通信编程的同步实现
- C#各种配置文件使用,操作方法总结
- C# 文件内容的操作
- C#中泛型容器Stack<T>的用法,以及借此实现”撤销/重做”功能
- c# 类成员的可访问性
- C#特性的介绍及应用场景