Things based on operator overloading
- 2020-05-10 18:44:16
- OfStack
Because of the operator overload is not how understanding ah, so baidu 1, the results found that an explanation is very interesting baidu know, share to see.
Answer:
+-*/ Such operator redefines, for example, if you customize it 1 A class a, And then you can override its operators yourself, for example + Return what, - Return something and so on.
public class a{
public string t{get;set;}
public static a operator +(a a1, a a2)
{
a b=new a();
b.t=a1.t+a2.t;
return b;
}
}
Roughly.
Follow-up:
Heroes, really do not see understand, can be more popular
Answer:
Like this, if you have now created 1 A custom class called Apple, normally, if you define 2 A variable apple A And apple B Instantiate them , And you want to know apple A- apple B What do you get, by default 2 You can't directly manipulate a variable if you're going to implement it apple Of the class "-" Operation, then you have to overload it The operator "-" , and then implement your own operations in it. Such as You can achieve by saying that two different dishes subtract fruit and return 1 One rotten apple or return null
Follow-up:
Warrior, can you say the apple class, on its" - "Operator overload, the result is two apples of poor quality, please write this Demo, You'd better write in detail, add a note or something, thank you, just take an apprentice,
(*^__^*) Hee hee...
Answer:
public class Apple{
public decimal Weight{get;set;}
public static decimal operator +(Apple a1, Apple a2)
{
return a1.Weight-a2.Weight;
}
}
Apple a=new Apple{Weight=200};
Apple b=new Apple{Weight=300};
Console.Write(b-a);
Try this no.
Follow-up:
Thank you. In order to perfect this q&a, I will modify the respondents 1 Some omissions:
Above code code 3 Ok, it should be" - ", correct me
public static decimal operator - (Apple a1, Apple a2)
The following 3 Sentence should be put in Main() In the function, this is perfect
---------------------------------
Apple a=new Apple{Weight=200};
Apple b=new Apple{Weight=300};
Console.Write(b-a);
--------------------------------
2 Floor:
I think upstairs two say very good, very clear, I change 1 In one way or another 1 Under.
Want to answer the building Lord's question, so want to be clear first 2 Three concepts, operator, overload
The operator
The operator refers to + , - , * , / Symbols for arithmetic operations, symbols that are written in the program code that our compiler can recognize because we've defined the rules for the compiler, so you can see that there are some things that you can use + Connect, and some do not, that is because the system has no custom such rules.
overloading
overloading 1 A programming language allows multiple names to exist 1 Sample, but parameter not 1 Sample method (function). Jot down two methods to look at:
int Add(int a,int b);
double Add(double a,double b);
We defined two Add Methods, but their arguments don't 1 If it's legal, then we can say it Add Methods have 2 A "reload".
Now, suppose you take Add The method is viewed as" + "Sign, then the operator overloading means," + "Sign can be added with two modifiers ( int ), or add two doubles ( double ) number, is because of the operator overload this property!
By operator overloading, you can make" + "Let all the things add up 1 Up!
3 Floor:
The example has been given to you. Let me give it to you in a colloquial way.
We all know that 1+1=2 Because that's what mathematicians say, and that's what we learned. And as long as you are here on earth, no one, unless he is ignorant and boring, will be able to disprove this conclusion.
Such universal laws as these have been accepted by the broad masses of the people 1 The designers of programming languages will certainly follow common sense in designing languages. So in any case 1 In the language 1+1=2 Both are true. Program can recognize 1 with 2 It's two Numbers. It's still recognizable + . - . x The present 4 The operation. General numerical 4 So the calculation is going to be consistent with people's normal thinking habits to get the right result. The reason a computer can process Numbers correctly 4 Computing, after all, is not because computers have artificial intelligence. It's that the language designer has designed the program to interpret the rules. The computer just processes the data according to this rule.
design 1 When it comes to a language, only the most general laws can be implemented, and those special laws need to be implemented by the makers of these laws.
For instance "Horse + The horse = The horse "; "The ass + The horse = The mule" This may still be common sense. There may be freaks who define "people. + demon = Transvestite "...
It's good enough for a computer to recognize Numbers. You wouldn't expect him to know what a horse is. At this point you define 1 The name of the class is "horse". And I wrote it in there 1 Some "horse" attributes. Gradually the image of the horse grew. The computer can finally recognize the horse. But "horse" instantiates out of the object it is not a number, the computer does not know "horse" + What does "horse" mean, and that's when you need to define "horse" 4 The operation. So the computer knows the stallion again + "Mother horse" = "Foal." This is operator overloading for a class of objects.
Then you define the "donkey" class. The computer also knows "donkey".
Finally you need to define "donkey. + The horse = "Mule", this is a different kind of object operator overload.
Actually, string The object's + Operation is the simplest and most commonly used operator overload. even int+double in C# Is also to be defined.