下面的代码如何工作以及我在哪里使用它? [英] How does the below code work and where would I use it?
问题描述
您好
有人可以向我解释以下代码:
protected void Clone (项目项目)
{
if(item == null)
return;
this.ID = item.ID;
this.NAME = item.NAME;
this.COLOR = item.COLOR;
}
public int CompareTo(对象其他)
{
if(other == null)
{
返回1;
}
返回this.NAME。 CompareTo(((Item)other).NAME);
}
公共覆盖字符串ToString()
{
返回string.Format("名称:{0},颜色:{1}",NAME,COLOR);
}
public override int GetHashCode()
{
返回this.ID.GetHashCode();
}
Hi
Can some one please explain to me the following code:
protected void Clone(Item item)
{
if (item == null)
return;
this.ID = item.ID;
this.NAME = item.NAME;
this.COLOR = item.COLOR;
}
public int CompareTo(object other)
{
if (other == null)
{
return 1;
}
return this.NAME.CompareTo(((Item)other).NAME);
}
public override string ToString()
{
return string.Format("Name: {0}, Color: {1}", NAME, COLOR);
}
public override int GetHashCode()
{
return this.ID.GetHashCode();
}
推荐答案
你知道解释了多少工作吗?逐行代码是?
每一行都需要一段解释!例如:
Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();
创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。
可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?
不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 你想坐下45分钟然后输入逐行描述没有充分理由吗?
此外 - 代码相当漂亮除了做作业问题外,还有很多自我解释!
Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.
Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?
No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
Besides - that code is pretty much self explanatory, except as a homework question!
这篇关于下面的代码如何工作以及我在哪里使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!