当前位置:网站首页>C # use this keyword to concatenate constructor to call method
C # use this keyword to concatenate constructor to call method
2022-07-19 04:13:00 【Curz crisp】
This article gives an example of C# Use this Keyword implements the concatenation constructor to call the method . Share with you for your reference . The specific analysis is as follows :
If you need to implement multiple custom constructors in a class , The usual approach is to implement the respective business logic in the constructor , If the implementation of these business logic is not entirely different , Obviously not in line with oop Programming idea , Extremely difficult to maintain , Of course , We can also encapsulate the same logic into a method , But there is a more reasonable and simple way , Let's go through this Keyword to implement the concatenation constructor to do a simple example .
The sample code is as follows :
public class Person
{
public string personName;
// Define age as nullable type , So you can give it null value
public int? personAge;
// The first three constructors below are the fourth constructor with the most parameters to call , Just take some of the parameters they need
// The way to do it is this Concatenation constructor
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;
// adopt ?? Judge the incoming age whether null value
// If it belongs to null value , Then assign 100
this.personAge = age ?? 100;
}
public void Display()
{
Console.WriteLine("Name:{0},Age:{1}\n", personName, personAge);
}
}
The main function call is as follows :
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 approach is to let a constructor that accepts the most parameters do " Primary constructor ", And implement the necessary business logic in the main constructor , The rest of the constructors just use this Keyword forwards the passed in parameters to the main constructor , And provide other necessary parameters , It looks like , What we need to worry about in our whole class is the main constructor , Other constructors can basically be empty .
Be careful : If there is logic to implement each other in the constructor chain , In fact, it is the code that executes the main constructor first , Then execute their respective logic , Use this method , The real work is left to a constructor , Class definitions will be simpler 、 Easier to maintain 、 Simplifies programming tasks .
边栏推荐
- 论文精读系列文章
- Build a portrait matting server based on openvino model server
- Wechat online education video on demand learning applet graduation project (4) opening report
- 机器学习11:代价敏感学习
- 【超能云终端创领先机】本地计算云端管理,英特尔助力教育数字化
- Some problems after xcode11 new project
- 软件测试-进阶篇
- PAC Decade: witness HPC from CPU era to XPU Era
- Awesome. It turns vscode into idea. It's a little wow
- 7.16 simulation summary
猜你喜欢

让程序员早点下班的《技术写作指南》

Awesome. It turns vscode into idea. It's a little wow

sql界面切换不能获取焦点

Matlab drawing activation function sigmoid, relu

Timeline components

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

Skillfully use enterprise network disk to collect reports or summaries

Wechat online education video on demand learning applet graduation design (1) development outline

Chapter 1 performance platform godeye source code analysis - overall architecture

Introduction to PLC OPC information model (DI, PLCopen nodesets)
随机推荐
In tech 2022 | Intel technology product innovation quick view
Large file upload
Academic sharing | design and development of multi staining pathological image information evaluation system based on openvino
[database] must know at the end of the term ----- Chapter 6 experiment
How to use Google Earth client and KML Download
Leetcode 931: minimum sum of descent path
Mathematical modeling learning (67): detailed introduction to xgboost classification model case tutorial
《云智面对面》直播等你来: 算力重新定义生产力
hello world驱动
sql界面切换不能获取焦点
Dapr series (I)
英特尔+联想共同推出开源云解决方案
Wechat online education video on demand learning applet graduation design (1) development outline
【超能云终端创领先机】本地计算云端管理,英特尔助力教育数字化
minimum spanning tree
Typescript数组/对象/字符串/函数参数的解构使用
小程序毕设作品之微信电子书阅读小程序毕业设计(2)小程序功能
Chapter 1 performance platform godeye source code analysis - overall architecture
Laradock restart MySQL found
【黄啊码】MySQL入门—5、数据库小技巧:单个列group by就会,多个列呢?