也可以用this来做为参数状当前实例做为参数传入方法.
还可以通过this[]来声明索引器
下面是你这段程序的注解:
// 引入使命空间System
using System;
// 声明命名空间CallConstructor
namespace CallConstructor
{
int petalCount = 0;
String s = "null";
Car(int petals)
// Car类的默认构造函数中为 petalCount 赋值为传入的参数petals的值
petalCount = petals;
// 输出petalCount
Console.WriteLine("Constructor w/int arg only,petalCount = " + petalCount);
// 重载Car类的构造函数
// : this(petals) 表示从当前类中调用petals变量的值来作为构造函数重载方法Car(String s, int
Car(String s, int petals) : this(petals)
{
this.s = s;
Console.WriteLine("String & int args");
}
// 重载构造函数,: this("hi", 47) 表示调Car(String s, int petals) 这个重载的构造函数,并直接传入
Car() : this("hi", 47)
{
}
public static void Main()
{
}
}
}
相关新闻
- 小程序登录流程图理解 2020-08-18
- 在C#中获取web.config中的配置信息 2021-08-23
- 小程序open-data头像样式 2021-04-10
- 小程序rich-text 富文本解析图片过大和图片路径的问题 2020-11-25
- C#中去掉字符串的最后一个字符 2020-11-23