一、构造函数

  构造函数是类的特殊方法,它永远不会返回值(即使是void),并且方法名和类名相同,同样支持重载。在使用new关键字创建对象时构造函数被间接调用,为对象初始化字段和属性的值。

  无参构造函数即默认构造函数,在我们没有创建任何构造函数时,编译器会为我们创建一个默认构造函数,反之当我们创建了任何构造函数,编译器不会为我们创建默认构造函数。

  下面我们设计一个无构造函数的空调类,一个无默认构造函数的空调类和一个有多个构造函数的空调类,并实例化它们。

 /// <summary>
/// 空调Air
/// </summary>
public class Air
{
#region 构造函数
//默认构造函数的结构
//public Air() { }
#endregion #region 字段
/// <summary>
/// 空调温度
/// </summary>
public int temperature; /// <summary>
/// 空调上下方向(用int量化方向,0代表下,1代表中下,2代表中,3代表中上,4代表上)
/// </summary>
public int verticalDirection; /// <summary>
/// 空调开关(用int量化开关,0代表关,1代表开)
/// </summary>
public int switch_C;
#endregion
}
/// <summary>
/// 空调AirOther
/// </summary>
public class AirOther
{
#region 构造函数
/// <summary>
/// 一个参数构造函数
/// </summary>
/// <param name="num"></param>
public AirOther(int num)
{
//verticalDirection和switch_C会被设置为默认值0。
temperature = num;
} /// <summary>
/// 多参构造函数
/// </summary>
/// <param name="openOrClose">0代表关,1代表开</param>
/// <param name="upOrdown">0代表下,1代表中下,2代表中,3代表中上,4代表上</param>
/// <param name="num">空调温度</param>
public AirOther(int openOrClose, int upOrdown, int num)
{
temperature = num;
verticalDirection = upOrdown;
switch_C = openOrClose;
}
#endregion #region 字段
/// <summary>
/// 空调温度
/// </summary>
public int temperature; /// <summary>
/// 空调上下方向(用int量化方向,0代表下,1代表中下,2代表中,3代表中上,4代表上)
/// </summary>
public int verticalDirection; /// <summary>
/// 空调开关(用int量化开关,0代表关,1代表开)
/// </summary>
public int switch_C;
#endregion
}
/// <summary>
/// 空调AirConditioner
/// </summary>
public class AirConditioner
{
#region 构造函数
/// <summary>
/// 无参构造函数
/// </summary>
public AirConditioner()
{
temperature = ;
verticalDirection = ;
switch_C = ;
} /// <summary>
/// 一个参数构造函数
/// </summary>
/// <param name="num"></param>
public AirConditioner(int num)
{
//verticalDirection和switch_C会被设置为默认值0。
temperature = num;
} /// <summary>
/// 多参构造函数
/// </summary>
/// <param name="openOrClose">0代表关,1代表开</param>
/// <param name="upOrdown">0代表下,1代表中下,2代表中,3代表中上,4代表上</param>
/// <param name="num">空调温度</param>
public AirConditioner(int openOrClose, int upOrdown, int num)
{
temperature = num;
verticalDirection = upOrdown;
switch_C = openOrClose;
}
#endregion #region 字段
/// <summary>
/// 空调温度
/// </summary>
public int temperature; /// <summary>
/// 空调上下方向(用int量化方向,0代表下,1代表中下,2代表中,3代表中上,4代表上)
/// </summary>
public int verticalDirection; /// <summary>
/// 空调开关(用int量化开关,0代表关,1代表开)
/// </summary>
public int switch_C;
#endregion
}
/// <summary>
/// 实现对象
/// </summary>
public class RealizeObject
{
public void Realize()
{
//Air类无构造函数,创建airNotConstructor对象时会调用默认构造函数为它的所有int型字段赋默认值0。
Air airNotConstructor = new Air(); //AirOther类不能使用new AirOther()创建对象,因为当我们创建了任何构造函数后,编译器不会为我们创建默认构造函数,此时如果想使用默认构造函数应显示创建。
//AirOther airNotDefaultConstructor = new AirOther(); //在创建对象air过程中,调用的是AirConditioner类的无参构造函数,它为air的字段赋如下代码的值:
//air.temperature = 28;
//air.verticalDirection = 3;
//air.switch_C = 0;
AirConditioner air = new AirConditioner(); //在创建对象airOne过程中,调用的是AirConditioner类的一个参数构造函数,它为对象的temperature赋值21,其他两个int型字段被赋默认值0。
AirConditioner airOne = new AirConditioner(); //在创建对象airOpen18过程中,调用的是AirConditioner类的多参构造函数,并赋对应字段传入的值。
AirConditioner airOpen18 = new AirConditioner(, , );
}
}

  默认构造函数为数据类型赋默认值如下:

  bool类型设置为false,

  数值类型设置为0或者0.0,

  char类型设置为单个空字符,

  DataTime类型设置为1/1/0001 12:00:00 AM,

  对象引用(包括string)设置为null。

二、this关键字

  this关键字的作用之一是代表当前类,在方法的参数名与类的字段或属性名相同时,通过this.XXX代表类的字段或属性名,与方法的参数名区分开来。this关键字的作用之二是简化多构造函数中冗余的逻辑代码。

 /// <summary>
/// 洗衣机的类
/// </summary>
public class WashingMachine
{
/// <summary>
/// 时长(单位:分钟)
/// </summary>
public int duration; /// <summary>
/// 衣物的总数量
/// </summary>
public int clothesNum; public WashingMachine() { } public WashingMachine(int duration)
{
if (duration > )
{
duration = ;
}
//通过this关键字区分类的字段名与构造函数的参数名
this.duration = duration;
} public WashingMachine(int duration, int clothesNum)
{
if (duration > )
{
duration = ;
}
//通过this关键字区分类的字段名与构造函数的参数名
this.duration = duration;
this.clothesNum = clothesNum;
}
}

使用构造函数给洗衣机时长赋初始值时,超过60分钟的时长统一等于60分钟,此时多构造函数逻辑代码冗余。通过构造函数链的方式,以最多参数的构造函数为主函数,让其他构造函数通过最多参数的构造函数来初始化数据。

 /// <summary>
/// 洗衣机的类
/// </summary>
public class WashingMachine
{
/// <summary>
/// 时长(单位:分钟)
/// </summary>
public int duration; /// <summary>
/// 衣物的总数量
/// </summary>
public int clothesNum; public WashingMachine() { } public WashingMachine(int duration) : this(duration, ) { } public WashingMachine(int clothesNum) : this(, clothesNum) { } public WashingMachine(int duration, int clothesNum)
{
if (duration > )
{
duration = ;
}
//通过this关键字区分类的字段名与构造函数的参数名
this.duration = duration;
this.clothesNum = clothesNum;
}
}

注意:当调用构造函数WashingMachine(int duration)时,会先进入主构造函数,主构造函数执行完成后,会返回到WashingMachine(int duration)构造函数执行其代码。

C#类型成员:构造函数的更多相关文章

  1. 【.net 深呼吸】细说CodeDom(5):类型成员

    前文中,老周已经厚着脸皮介绍了类型的声明,类型里面包含的自然就是类型成员了,故,顺着这个思路,今天咱们就了解一下如何向类型添加成员. 咱们都知道,常见的类型成员,比如字段.属性.方法.事件.表示代码成 ...

  2. java类定义、变量类型、构造函数

    1.java类class的定义 所有java程序都以类class为组织单元,java类由属性和方法组成,下面看例子: public  class  Phone{ //属性 String company ...

  3. NET设计规范二:类型成员设计

    http://www.cnblogs.com/yangcaogui/archive/2012/04/20/2459567.html 接着 → .NET设计规范一:设计规范基础 上一篇,我们来了解下类型 ...

  4. C++ static、const和static const类型成员变量声明以及初始化

    C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. sta ...

  5. C#反射与特性(五):类型成员操作

    目录 1,MemberInfo 1.1 练习-获取类型的成员以及输出信息 1.2 MemberType 枚举 1.3 MemberInfo 获取成员方法并且调用 1.4 获取继承中方法的信息(Decl ...

  6. F2工作流引擎参与者类型成员的交、并、互拆计算规则

          计算描述:计算规则指的是和其它“参与者类型成员”的之间的计算,必须求解处理人不为空的情况下才进行规则计算,各个“参与者类型成员”按序号顺序执行. 计算算法:并集(权重最低),交集(权重中) ...

  7. 请为CMyString类型编写构造函数、copy构造函数、析构函数和赋值运算符函数。

    如下为类型CMyString的声明,请为该类型编写构造函数.copy构造函数.析构函数和赋值运算符函数. class CMyString { public: CMyString(const char* ...

  8. c++ string类型成员变量在调用构造函数后未能正确赋值

    struct RelItem{ string segName; Elf32_Rel* rel; string relName; RelItem(string seg, int addr, string ...

  9. 【原】结构体包含CString类型成员变量出错的原理

    问题如下:我定义了如下的一个结构体: typedef struct{   CString csText;}MyStruct; 并有如下的程序段1:MyStruct * p=NULL;p=(MyStru ...

随机推荐

  1. 浅谈 HTTP协议

    1.什么是http协议Hyper Text Transport Portocal(超文本传输协议)HTTP协议是应用层协议浏览器和web服务器通讯时遵守的约定互联网使用最多的协议提供超文本的传输服务通 ...

  2. WebSocket专题(阿里)

    我们的项目中使用了websocket 用java-websocket 开源项目做的,阿里的人问我用啥实现的websocket一时没有答上来 回来做了总结: 1.前言 最近有同学问我有没有做过在线咨询功 ...

  3. Spring中为什么继承了ApplicationContextAware接口就可以使用ApplicationContext对象?

    1.Spring中使用applicationContext对象 public class SpringContextUtil implements ApplicationContextAware { ...

  4. 内存映射文件MappedByteBuffer和Buffer的Scattering与Gathering

    上一篇讲到的DirectByteBuffer继承自MappedByteBuffer 一.MappedByteBuffer MappedByteBuffer的定义: A direct byte buff ...

  5. hive函数 get_json_object

    pandas和SQL数据分析实战 https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2&sha ...

  6. 【C++】C++中的异常解析

    异常是程序在执行期间产生的问题.C++ 异常是指在程序运行时发生的特殊情况,比如尝试除以零的操作. 异常提供了一种转移程序控制权的方式.C++ 异常处理涉及到三个关键字:try.catch.throw ...

  7. SNF快速开发平台2020版

    SNF快速开发平台分如下子平台: 1.CS快速开发平台 2.BS快速开发平台 3.H5移动端快速开发平台 4.软件开发机器人平台 配置型开发零编程 SNF快速开发平台是一个比较成熟的.net领域的商业 ...

  8. (转)CMDB介绍

    原文:https://www.cnblogs.com/xuecaichang/p/10265936.html CMDB开发---https://blog.csdn.net/bbwangj/articl ...

  9. continue & tag in GO

    Go语言中 continue 语句可以结束当前循环,开始下一次的循环迭代过程,仅限在 for 循环内使用,在 continue 语句后添加标签时,表示开始标签对应的循环,例如: package mai ...

  10. 中国大互联网公司在github上的开源项目

    公司名 账号数 账号名 总项目数 非fork项目数 百度 13 baidu.ApolloAuto. brpc. mipengine.Clouda-team.mesalock-linux. ecomfe ...