当前位置:网站首页>C# 使用this关键字串联构造函数调用方法
C# 使用this关键字串联构造函数调用方法
2022-07-17 03:25:00 【Curz酥】
本文实例讲述了C#使用this关键字实现串联构造函数调用方法。分享给大家供大家参考。具体分析如下:
在一个类中如果需要实现多个自定义构造函数,通常做法是在构造函数中实现各自的业务逻辑,如果这些业务逻辑的实现并非截然不同的话,显然不符合oop编程思想,极不利于维护,当然,我们也可以通过将相同的逻辑部分封装成一个方法,但还有一种更为合理简单的方法,下面就通过this关键字来实现串联构造函数做一简单示例。
示例代码如下:
public class Person
{
public string personName;
//定义年龄为可空类型,这样就可以赋予其null值
public int? personAge;
//下面前三个构造函数都是去调用参数最多的第四个构造函数,只取它们所需要的部分参数即可
//这样的做法就是this串联构造函数
public Person():this("",0)
{
}
public Person(string name):this("evan",null)
{
}
public Person(int age):this("",20)
{
}
public Person(string name, int? age)
{
this.personName = name;
//通过 ?? 判断传入的age是否null值
//如果属于null值,则赋值100
this.personAge = age ?? 100;
}
public void Display()
{
Console.WriteLine("Name:{0},Age:{1}\n", personName, personAge);
}
}
主函数调用如下:
static void Main(string[] args)
{
Person per1 = new Person();
per1.Display();
Person per2 = new Person(20);
per2.Display();
Person per3 = new Person("evan");
per3.Display();
Person per4 = new Person("evan", 20);
per4.Display();
Console.ReadLine();
}
这样的做法就是让一个接受参数最多的构造函数做"主构造函数", 且在主构造函数中实现必须的业务逻辑,其余的构造函数只要使用this关键字把传入的参数转发给主构造函数,并且提供必须的其它参数,这样子,我们整个类中需要我们操心的就是那个主构造函数了,其余构造函数基本上可以为空。
注意:如果构造函数链中还有实现各自的逻辑,那么实际上是先执行主构造函数的代码,再执行各自逻辑,使用这种做法,真正的工作都交给了一个构造函数,类定义就会更简洁、更易维护、简化了编程任务。
边栏推荐
- I'm 25 years old, but I still can't do anything. What if I can't find a way out? How about we media?
- Which computer painting software works well: try artweaver plus, which is comparable to Sai painting software | download the latest version of artweaver
- Typescript数组/对象/字符串/函数参数的解构使用
- [database] must know and be able at the end of the term ----- Chapter 10 database programming
- Program analysis and Optimization - 11 multi branch analysis
- Introduction to PLC OPC information model (DI, PLCopen nodesets)
- 【超能云终端创领先机】本地计算云端管理,英特尔助力教育数字化
- Use of anti shake debounce and throttling throttle
- PLC OPC 信息模型(DI,PLCopen NodeSets)简介
- 小程序畢設作品之微信在線教育視頻點播學習小程序畢業設計(3)後臺功能
猜你喜欢

库函数的模拟实现

Asp. Using grpc in NETCORE

Hcip Experiment 4

sql界面切换不能获取焦点

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(2)小程序功能

小程序毕设作品之微信电子书阅读小程序毕业设计(6)开题答辩PPT

Introduction to PLC OPC information model (DI, PLCopen nodesets)

Doris learning notes query

Idea configures SFTP and SSH, which is very convenient to deploy and locate error logs

FTXUI基础笔记(botton按钮组件基础)
随机推荐
[Paper Abstract] screenshots of methods for recording abstracts of interest and papers in special fields.
A Tutorial on Learned Multi-dimensional Indexes
小程序毕设作品之微信电子书阅读小程序毕业设计(5)任务书
Workload-Aware Performance Tuning for Autonomous DBMSs
Container adapter - stack, queue, priority queue
Unity Shader - “快速“ 次散射 (Fast SSS : Fast Subsurface Scattering)
小程序毕设作品之微信电子书阅读小程序毕业设计(3)后台功能
Styleflow concise reading: use continuous flow to complete attribute editing
Digital twinning - Chapter 2, digital twinning Technology
C'est génial de jouer vscode comme un effet idea.
小程序毕设作品之微信电子书阅读小程序毕业设计(7)中期检查报告
Chapter 2 - create and maintain MySQL database
小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(2)小程序功能
dapr系列(一)
7.16 simulation summary
[database] must know and be able at the end of the term ----- Chapter 1 database overview
Wechat online education video on demand learning applet graduation project (4) opening report
Klakndi synchronization screen is simple to use
donet framework4.X==windows窗体应用新建项目,通过System.Data.SqlClient连接sqlserver进行查询
【Notebook系列第七期】OpenVINO预训练模型的的下载和使用方法