answer:
+-*/ Such operators are redefined, for example, if you customize a class a, you can overload these operators of it yourself, such as + returns, - returns, etc.
Probably this means.
Follow-up question:
Hero, I really didn't understand it, can it be more popular
answer:
Like this, if you now create a custom class called Apple, under normal circumstances, if you define two variables, Apple A and Apple B, instantiate them, and then you want to know what Apple A-Apple B can get, the default these two variables cannot be directly computed. If you want to implement the "-" operation of the Apple class, then you must overload its operator "-" and then implement your operations in it yourself. For example, you can implement the subtraction of two different fruits and vegetables to return a rotten apple or return null
Follow-up question:
Hero, can you overload the "-" operator of the apple class you mentioned? The result is that the quality of the two apples is poor. Please write this demo. It is best to write a detailed description and add a comment or something. Thank you, just take an apprentice.
(*^__^*) Hehe...
answer:
public class Apple{
public decimal Weight{get;set;}
public static decimal operator +(Apple a1, Apple a2)
{
return ;
}
}
Apple a=new Apple{Weight=200};
Apple b=new Apple{Weight=300};
(b-a);
Try this or not.
Follow-up question:
Thank you, in order to make this question and answer more perfect, I will modify the answerer to some omissions:
The third line of the above code should be the "-" sign, correct it
public static decimal operator - (Apple a1, Apple a2)
The following three sentences should be placed in the Main() function, which is perfect
---------------------------------
Apple a=new Apple{Weight=200};
Apple b=new Apple{Weight=300};
(b-a);
--------------------------------
Second floor:
I think the two people above said it very well and it is very clear. I will explain it in another way.
To answer the poster's question, you must first clarify two concepts, operators, and overloading
Operator
Operators refer to symbols such as +, -, *, /, etc. used for arithmetic operations. These symbols are written in the program code and our compiler can recognize them because we have defined the rules (grammar) for the compiler, so you will find that some things can be connected with +, while some cannot. That is because the system does not customize such rules.
Reload
Overloading a language feature means that programming languages allow multiple methods (functions) with the same name but different parameters. There are two ways to write:
int Add(int a,int b);
double Add(double a,double b);
We have defined two Add methods, but their parameters are different, which is legal. At this time, we can say that the Add method has two "overloads".
Now, if you regard the Add method as the "+" sign, then the meaning of overloading of this operator is revealed. The "+" sign can add two integer numbers, or two double numbers, because of the characteristic of overloading of operators!
By operator overloading, you can make "+" add everything together!
Third floor:
Someone has given the example, I'll just listen to it in a simple way.
We all know that 1+1=2 is because mathematicians stipulate this and we learn it in this way. And as long as you are on earth, no one will come to overturn this conclusion unless he is ignorant and bored.
When designing a language, you can only implement the most common laws. As for those special laws, you need to implement them by the makers of these laws.
For example, "horse + horse = horse"; "donkey + horse = mule" may still be common sense. There may be freaks that define "human + demon = shemale"...
It is already very good for computers to recognize values. You won't expect it to know what a horse is. At this time you define a class, the name of the class is "horse". And wrote some attributes of "horse" in it. Gradually, the image of the landmas became fuller. Computers can finally recognize "horse". However, the object instantiated by "horse" is not a numerical value, and the computer does not know what "horse + horse" equals. At this time, you need to formulate four operations for "horse". So the computer learned about "stalk horse" + "mare" = "little foal". This is an operator overload for similar objects.
Then you defined the "donkey" class again. Computers also know "donkey".
Finally, you also need to define "donkey + horse = mule", which is the operator overloading of different types of objects.
In fact, the + operation of string object is the simplest and most commonly used operator overloading. Even int+double needs to be defined in C#.