有人可以解释一下代码。 [英] Can some one please explain the code.
本文介绍了有人可以解释一下代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以解释一下代码。
public Racer( int id, string firstName, string lastName, string country): this (id,firstName,lastName,country,wins: 0 ){}
我的尝试:
有人可以解释一下代码。
解决方案
在你的代码中this用于重载构造函数。this()语法指示编译器插入与指定参数列表匹配的指定构造函数。
例如:
public class 用户{
string firstName,l astName;
int 年龄;
public 用户(字符串 FName, string LName): this (FName,LName,Age = 0 )
{
firstName = Fname;
lastName = LName;
此 .Age =年龄;
}
}
class 计划
{
静态 void Main()
{
用户obj1 = new 用户( abc, xyz);
用户obj2 = 新用户( abc , xyz, 19 < /跨度>);
}
}
输出:
1 ] abc,xyz, 0
2 ] abc,xyz, 19
Can some one please explain the code.
public Racer(int id, string firstName, string lastName, string country) :this(id, firstName, lastName, country, wins: 0) { }
What I have tried:
Can some one please explain the code.
解决方案
In your code "this" is used overload the constructor.The this() syntax instructs the compiler to insert the specified constructor that matches the parameter list specified.
eg:
public class User{ string firstName,lastName; int Age; public User(string FName,string LName):this(FName,LName,Age=0) { firstName=Fname; lastName=LName; this.Age=Age; } } class Program { static void Main() { User obj1= new User("abc","xyz"); User obj2= new User ("abc","xyz",19); } } Output: 1] abc, xyz, 0 2] abc, xyz, 19
这篇关于有人可以解释一下代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文