新闻动态
新闻动态
- 301设置方法和转向代码
- 网站建设及优化要从四个方面斟酌
- append的用法
- Vue.prototype的使用
- el-form中ref的作用
- JS调用CS里的方法:PageMethods(荐)
- 网站优化的几点技巧
- 石家庄网站建设:关键词竞争度分析指南
- EF Core迁移 (provider: SSL Provider, error: 0 – 证书链是由不受信任的颁发机构颁发的) 错误
- 矩阵号是什么
联系我们
邮箱:
手机:15383239821
asp.net
c#中this的用法
作者:
发布时间:2010-10-04
点击:
在C#中,this关键字代表当前实例,我们可以用this.来调用当前实例的成员方法,变量,属性,字段等;
也可以用this来做为参数状当前实例做为参数传入方法.
还可以通过this[]来声明索引器
下面是你这段程序的注解:
// 引入使命空间System
using System;
// 声明命名空间CallConstructor
namespace CallConstructor
{
// 声明类Car
public class Car
{
// 在Car类中:
// 声明一个非静态的整型变量petalCount,初始值为0
// 未用Static声明的变量叫做静态变量,非静态成员属于
//类的实例,我们只能在调用类的构造函数对类进行实例化后才能通过所得的实例加"."来访问
int petalCount = 0;
// 声明一个非静态的字符串变量s,初始值为"null";
// 注意:s = "null"与s = null是不同的
String s = "null";
// Car类的默认构造函数
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
也可以用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
// petals)的第二个参数
Car(String s, int petals) : this(petals)
{
// 在构造函数中为s赋值
// 非静态成员可以在构造函数或非静态方法中使用this.来调用或访问,也可以直接打变量的名字,因此这
Car(String s, int petals) : this(petals)
{
//一句等效于s = s,但是这时你会发类的变量s与传入的参数s同名,这里会造成二定义,所以要加个this.表
//示等号左边的s是当前类自己的变量
this.s = s;
Console.WriteLine("String & int args");
}
// 重载构造函数,: this("hi", 47) 表示调Car(String s, int petals) 这个重载的构造函数,并直接传入
this.s = s;
Console.WriteLine("String & int args");
}
// 重载构造函数,: this("hi", 47) 表示调Car(String s, int petals) 这个重载的构造函数,并直接传入
//变量"hi"和47
Car() : this("hi", 47)
{
Console.WriteLine("default constructor");
}
public static void Main()
{
Car x = new Car();
Console.Read();
}
}
}
Car() : this("hi", 47)
{
}
public static void Main()
{
}
}
}
新闻资讯
-
2024-04-02require和require-dev的区别
-
2010-12-09怎么取域名,才有利于SEO?
-
2010-11-03从表中取出某年或某月的数据
-
2012-06-30百度快照对网站关键词排名的影响是什么
-
2010-07-20网页优化“URL循环pr”增加pr值理论
-
2011-04-01禁止网页另存为的代码
相关案例
