大侠们请帮忙看看

zlHandler 2009-11-23 02:39:05
public Teacher(string name) : this(name, 2, 5000) { }
这个this是啥意思?
...全文
56 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
showTI 2009-11-23
  • 打赏
  • 举报
回复
public class student
{
public student():this(10)//调用 下面一个带参数的构造函数构造函数(说明:this就是代表这个类本身。)
{

}
  public student(int temp){} //student类的一个构造函数
}
zlHandler 2009-11-23
  • 打赏
  • 举报
回复
简单点说。。。就举一个比如Student类的例子,我看看谁解释的好!
zuoming120 2009-11-23
  • 打赏
  • 举报
回复
d
zhenhua110 2009-11-23
  • 打赏
  • 举报
回复
不太清楚啊!!
guoliwang 2009-11-23
  • 打赏
  • 举报
回复
就你这几句代码就是楼上说的意思!

this:
第一个this的意思是调用Car(int petals)方法的属性petals。
第二个this的意思是实例化Car(String s, int petals)方法中的参数s(this.s = s)。
第三个this是调用Car(String s, int petals)方法的两个参数并传参。

在C#中,this关键字代表当前实例,我们可以用this.来调用当前实例的成员方法,变量,属性,字段等;
也可以用this来做为参数状当前实例做为参数传入方法.
还可以通过this[]来声明索引器

下面是你这段程序的注解:
using System; // 引入使命空间System
namespace CallConstructor // 声明命名空间CallConstructor
{
public class Car // 声明类Car
{
int petalCount = 0; //在Car类中声明一个非静态的整型变量petalCount,初始值为0
未用Static声明的变量叫做静态变量,非静态成员属类的实例,我们只能在调用类的构
造函数对类进行实例化后才能通过所得的实例加"."来访问声明一个非静态的字符串变量s,
初始值为"null"; 注意:s = "null"与s = null是不同的
String s = "null";
Car(int petals) // Car类的默认构造函数
{
petalCount = petals; // Car类的默认构造函数中为 petalCount 赋值为传入的参数petals的值
Console.WriteLine("Constructor w/int arg only,petalCount = " + petalCount); // 输出petalCount
}
Car(String s, int petals)// 重载Car类的构造函数 Car(String s, int petals)的第二个参数
: this(petals) // : this(petals) 表示从当前类中调用petals变量的值来作为构造函数重载方法
{
this.s = s;
Console.WriteLine("String & int args");
}
// 在构造函数中为s赋值
// 非静态成员可以在构造函数或非静态方法中使用this.来调用或访问,也可以直接打变量的名字,因此这一句等效于s = s,
//但是这时你会发类的变量s与传入的参数s同名,这里会造成二定义,所以要加个this.表示等号左边的s是当前类自己的变量
Car() // 重载构造函数,:
: this("hi", 47) // this("hi", 47) 表示调Car(String s, int petals) 这个重载的构造函数,并直接传入变量"hi"和47
{
Console.WriteLine("default constructor");
}
public static void Main()
{
Car x = new Car();
Console.Read();
}
}
}
asdfa23rdadsdfa 2009-11-23
  • 打赏
  • 举报
回复
this表示本身类
CXSilence 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hdt 的回复:]
调用另一个构造函数
类似

public class a
{
public a():this(10)//调用 a(int temp)
{
}
  public a(int temp){}
}
[/Quote]
string name) : this(name
真相重于对错 2009-11-23
  • 打赏
  • 举报
回复
调用另一个构造函数
类似

public class a
{
public a():this(10)//调用 a(int temp)
{
}
public a(int temp){}
}

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧