C#中构造函数的特定形式

时间:2022-11-17 15:55:18

I'm studying ICT. One of my courses is C# and another is Physics. Our Physics teacher used Visual Studio to animate some movements gave us some of the code he used to do it. He told us to look it up. Here's something I don't recognize:

我在学习ICT。我的课程之一是C#,另一门是物理。我们的物理老师使用Visual Studio动画一些动作给了我一些他用来做的代码。他告诉我们要查一查。这是我不认识的事情:

public static Vector operator + (Vector v1, Vector v2)
{
    return new Vector(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
}

That is supposed to be a constructor, but I've never seen anything like it before. I don't know what it's called, so I don't know what to google for.

那应该是一个构造函数,但我以前从未见过这样的东西。我不知道它叫什么,所以我不知道谷歌的用途。

Can someone enlighten me please?

有人可以开导我吗?

8 个解决方案

#1


It's not a constructor. It's an operator method for the '+' operator. It defines what happens when you do somethig like the following:-

它不是构造函数。它是'+'运算符的运算符方法。它定义了当您执行以下操作时会发生什么: -

var Vector v1 = new Vector(1,2,3);
var Vector v2 = new Vector(1,2,3);
var Vector v3 = v1 + v2;

#2


That is called "operator overloading". It is not a constructor but returns a new Vector.

这称为“运算符重载”。它不是构造函数,但返回一个新的Vector。

See: Operator Overloading

请参阅:运算符重载

#3


That's overloading the '+' operator for the Vector class so whenever you do 'v1 + v2' that code gets executed.

那就是为Vector类重载'+'运算符,所以每当你执行'v1 + v2'时代码就会被执行。

#4


He's overloading the "+" operator for the Vector class. See here for more information on the topic.

他正在为Vector类重载“+”运算符。有关该主题的更多信息,请参见此处。

#5


This isn't a constructor but an operator overload for + This is how you overload the behaviour for:

这不是一个构造函数,而是一个运算符重载+这是你重载行为的方式:

Vector a = new Vector();
Vector b = new Vector();
Vector c = a + b;

More info at the MSDN article.

有关MSDN文章的更多信息。

#6


The Line:

public static Vector operator + (Vector v1, Vector v2)

public static Vector operator +(Vector v1,Vector v2)

...is not a constructor. It's simply overloading the + (Plus) operator for the parent class. I assume the parent class is a refined vector class?

......不是构造函数。它只是为父类重载+(加号)运算符。我假设父类是一个精炼的矢量类?

The line:

return new Vector(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);

返回新的Vector(v1.X + v2.X,v1.Y + v2.Y,v1.Z + v2.Z);

...Is using the constructor of the Vector class to pass three arguments (X, Y, and Z it looks like)

...使用Vector类的构造函数传递三个参数(X,Y和Z看起来像)

#7


It is overloaded + operator. All OOP languages (except Java perhaps, need to check reference) allow operator overloading.

它是重载+运算符。所有OOP语言(Java除外,可能需要检查引用)允许运算符重载。

#8


He is indeed overloading the + operator.

他确实超载了+运算符。

what does this mean? well it's simple:

这是什么意思?这很简单:

if you have this:

如果你有这个:

int i = 10;
int j = 20;
int x = i + j;

In this example x would be 30 because C# knows if we are using integers and use + that we need to take the sum.

在这个例子中,x将是30,因为C#知道我们是否使用整数并使用+我们需要得到总和。

But now you are working with Vectors. A 3dimensional vector has 3 values: X, Y and Z. If you want the sum of 2 vectors well how do you work? does it go like

但现在你正在使用矢量。三维向量有3个值:X,Y和Z.如果你想要2个向量的总和,你如何工作?会不会像

v1.X + v2.Y and v1.Y + v2.Z and v1.Z + v2.X  

or should C# do it like this:

或者C#应该这样做:

v1.X + v1.Y and v1.Z + v2.X and v2.Y + v2.Z

With operator overloading you define how the + operator should be implemented when using Vectors. In our case it's:

使用运算符重载时,您可以定义在使用向量时应如何实现+运算符。在我们的例子中它是:

v1.X + v2.X and v1.Y + v2.Y and v1.Z + v2.Z 

not that hard ;)

不那么难;)

#1


It's not a constructor. It's an operator method for the '+' operator. It defines what happens when you do somethig like the following:-

它不是构造函数。它是'+'运算符的运算符方法。它定义了当您执行以下操作时会发生什么: -

var Vector v1 = new Vector(1,2,3);
var Vector v2 = new Vector(1,2,3);
var Vector v3 = v1 + v2;

#2


That is called "operator overloading". It is not a constructor but returns a new Vector.

这称为“运算符重载”。它不是构造函数,但返回一个新的Vector。

See: Operator Overloading

请参阅:运算符重载

#3


That's overloading the '+' operator for the Vector class so whenever you do 'v1 + v2' that code gets executed.

那就是为Vector类重载'+'运算符,所以每当你执行'v1 + v2'时代码就会被执行。

#4


He's overloading the "+" operator for the Vector class. See here for more information on the topic.

他正在为Vector类重载“+”运算符。有关该主题的更多信息,请参见此处。

#5


This isn't a constructor but an operator overload for + This is how you overload the behaviour for:

这不是一个构造函数,而是一个运算符重载+这是你重载行为的方式:

Vector a = new Vector();
Vector b = new Vector();
Vector c = a + b;

More info at the MSDN article.

有关MSDN文章的更多信息。

#6


The Line:

public static Vector operator + (Vector v1, Vector v2)

public static Vector operator +(Vector v1,Vector v2)

...is not a constructor. It's simply overloading the + (Plus) operator for the parent class. I assume the parent class is a refined vector class?

......不是构造函数。它只是为父类重载+(加号)运算符。我假设父类是一个精炼的矢量类?

The line:

return new Vector(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);

返回新的Vector(v1.X + v2.X,v1.Y + v2.Y,v1.Z + v2.Z);

...Is using the constructor of the Vector class to pass three arguments (X, Y, and Z it looks like)

...使用Vector类的构造函数传递三个参数(X,Y和Z看起来像)

#7


It is overloaded + operator. All OOP languages (except Java perhaps, need to check reference) allow operator overloading.

它是重载+运算符。所有OOP语言(Java除外,可能需要检查引用)允许运算符重载。

#8


He is indeed overloading the + operator.

他确实超载了+运算符。

what does this mean? well it's simple:

这是什么意思?这很简单:

if you have this:

如果你有这个:

int i = 10;
int j = 20;
int x = i + j;

In this example x would be 30 because C# knows if we are using integers and use + that we need to take the sum.

在这个例子中,x将是30,因为C#知道我们是否使用整数并使用+我们需要得到总和。

But now you are working with Vectors. A 3dimensional vector has 3 values: X, Y and Z. If you want the sum of 2 vectors well how do you work? does it go like

但现在你正在使用矢量。三维向量有3个值:X,Y和Z.如果你想要2个向量的总和,你如何工作?会不会像

v1.X + v2.Y and v1.Y + v2.Z and v1.Z + v2.X  

or should C# do it like this:

或者C#应该这样做:

v1.X + v1.Y and v1.Z + v2.X and v2.Y + v2.Z

With operator overloading you define how the + operator should be implemented when using Vectors. In our case it's:

使用运算符重载时,您可以定义在使用向量时应如何实现+运算符。在我们的例子中它是:

v1.X + v2.X and v1.Y + v2.Y and v1.Z + v2.Z 

not that hard ;)

不那么难;)