2.3.2 Overload constructor:
I still give you the concept first, let you have a knowledge, then we will discuss.
In Java:
1. Function overload is a method in which a plurality of synonyms are declared, but there are different parameters and parameter types.
2. Function Reconstruction refers to the method of declaring the same name as the parent class in the subclass, thereby covering the method of the parent class. Refactoring the problem of subclass and parent class. (I will explain in detail when discussing into inheritance)
In C :
1. The same concept.
2. The concept of reconstruction can be different, and the function of C is a large virtual function. More details here is not bad!
In fact, you are not stranger on the concept of overload, and I believe you have also touched in programming. Ha ha! Let us take an example of an operator to overload an example you will understand, (this feature does not support Java) We define three integer variables:
INT I1 = 2, I2 = 3, i3 = 0; i3 = i1 i2; at this time I3 = 5; the plus sign implements two number added calculation functions. However, we now have to define three string variables: string str1 = "jing", str2 = "wei", str3 = ""; str3 = str1 str2;
At this time, Str3 = "jingwei"; plus signage implements two string added calculation functions. It is also a plus sign, which can be added with both integer variables or add two string type variables. The same operator achieves different features ------ this is the so-called operator overload (嘿嘿, I said you must see it :)! It's not like the words in Chinese! What I need to explain is that the operator overload in C is not as simple as it is. For example, we can add operations to two customized objects to assign values. This is clearly clear, and it is very practical. Of course, there are too many topics overloaded with operators, interested in reading books!
We have transferred the topic of the operator to the function, and we have always emphasized the "object adjustment method" ------ Objects actually tune the "name" of the method. And now we have to make an overload, which is to define multiple identical names, so that the computer will not be confused when calling? I think it shouldn't, huh, huh, because it is just the same name, and we pass the parameters to him when calling the function. There is no parameter is also information of a parameter transmission parameter (information is not parameters)! However, due to the parameter type, the number of parameters, the return value type, we can distinguish the function of the same name! The purpose is only one, and more functions are implemented in a simple way. Still give an example, overload the constructor!
Public Class Employee {Public Employee (String N, Int A, CHAR S, FLOAT E, BOOLEAN L) {// Look at this constructor Name = n; // Set employee name AGE = a; // Set employee SEX = S ; // Set employee gender emolument = E; // Set employee salary lunch = L; // Set employee lunch} public Employee () {// Please note that this function does not have parameters name = "jw"; agent; SEX = 'W'; emolument = 99; lunch = true} // ..., etc.}; see, there are two names of the same name in a class, however we know how we call us when we use it. What about functions? Oh, I just said, can be determined by the parameter type, number of parameters, and return value types. Now let's test, we create a constructor of the two objects that call the band parameters, and the second object calls the default constructor. Let's take a look at the results:
Jingwei = New Employee ("jingwei", 20, 'm', 100, false); / * Call when creating this object is a constructor with parameters * / FlashMagic = New Employee (); // Creating this object is What is the result of the call to the value of the constructor? Let us take a look! JINGWEI This object: FlashMagic This object:
Name Jingwei Name JW
AGE 20 AGE 20 SEX M SEX W Emolument 100 Emolument 99 Lunch False Lunch True
Look, although two functions are identical, different work content is completed. Ha ha! About the function is overloaded here, I believe that you already have a big impression, and more detailed content still requires your efforts!
The heavy-duty of ordinary functions is similar to the heavy-duty constructor, but he has a THIS pointer! THIS is generally a reference to the current object. Let's say this, use this pointer if you tend to more than two objects. Each member function has a THIS pointer, it is a hidden parameter, the THIS pointer only calls its object! I said that there is only one way, and the object has its own properties. When the object call method is to be attribute, how can he determine that the call is not his own attribute? This requires the THIS pointer to the greatness.
About copy constructor, inline functions, virtual functions, template, etc., don't do too much, because Java doesn't seem to be there. However, I need to remind you that in C , the function defined in the class is automatically converted to the inline function, which seems to have conflicts with the idea mentioned earlier. Because the purpose of the inline function is to reduce the overhead of the function call! Ha ha! I haven't wound it! Please also give a prawn to each other! thank!