Object-oriented programming, my thoughts (below)

zhaozj2021-02-16  58

2.3 In-depth exploration of functions: 2.3.1 Constructor, default constructor, default constructor

For the above example, it can have the most of the work, but it is still imperfect, there are many details waiting to we will improve! Perhaps some classmates have noticed that when I created this object of "jingwei", all the properties of this object are empty, that is, the name of this object is undecided, age is undecided, sex is not fixed , Salary is undecided, lunch has not been determined. And we want to add these attributes, but also use object to call the corresponding method, go to one by one! God, this is too much trouble! Is there any good way to complete the operation of the property assignment while we create an object? Oh, it should be said to be the initialization of attributes? Of course, there is no problem, this requires a so-called constructor! The constructor is the most special function in the class, which is just the opposite of the designer function! From the feature: 1. It is the only function in the programming language without the type of return value. 2. Its name and class name must be exactly the same. 3. It must be declared as a public (public) type 4, which can be reloaded. 5. It is automatically called in creating an object. From functional: 1. It is initialized to attributes in the class. In fact, we don't have your own constructor for the above programs. However, in this case, the system will automatically define a "default constructor". He will automatically assign the numerical variable to 0, assign the Boolean variable to false, etc. (, in C , the default constructor does not initialize its members). If the programmer defines the constructor, the system will not add a default function to your program. (Here, we advocate yourself define the constructor, not the system's default constructor) or look at an instance! This is more clear!

//employee.java public class employee {private String name; // employee name private int age; // employees aged private char sex; // staff gender private float emolument; // staff salaries private boolean lunch; // staff lunch / / ... Wait Public Employee () {// This is the "default" constructor name = "jw"; // Set the employee name AGE = 20; // Set employee age SEX = "m"; // Set employee gender Emolument = 100; // Set employee salary lunch = false; // Set employee lunch} public void heater () {// This method is used to process lunch lunch = true;} // ..., etc.};

This way, while we create "jingwei" objects, all of its properties are also initialized! Obviously, this greatly improves working efficiency, but it still does not meet the requirements. Think about it, what happens if we now create this type of second object? Tell you, in addition to the "name" of the object (this name is not the name in the object property, but the name of the object itself) is not the same, all of its "property value" is the same! For example: Now we create a second object flashmagic, but I will find all the properties of this object and all the properties of the object of Jingwei are exactly the same. And we can only change write properties with objects! Obviously, this method is not good! We need a method to give "We want" when creating an object. I believe that you also see it, the default constructor is unable to force. What we need is a constructor with parameters. When you create an object, we pass the parameters to the constructor so that the above features can be completed! No mouth, or come to see an example: //employee.java public class Employee {private string name; // Employee Name Private Int Age; // Employee Age Private Char Sex; // Employee Gender Private Float Emolument; / / Employee Salary Private Boolean Lunch; // Employee Lunch // ... Wait 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 age SEX = S; // Set employee gender emolument = e; // Set employee salary lunch = L; // Set employee lunch} public void heater ()} This method is To process the lunch lunch = true;} // ......, etc.

In this way, we can give him the value we want, it is obvious that this can be more convenient. Oh, right! Haven't told you how to create it! Haha, turn a few pages forward you will see this sentence: jingwei = new Employee (); this is an object, and we change it into jingwei = new Employee ("jingwei", 20, 'm, 100 , false; this, all work is completed, huh, huh! (In the creation of the "initial value") 2.3.2 overload constructor: I still give you the concept first, let you have a knowledge, then we are discussed. In Java: 1. Function overload is a method that declares multiple as many names in a class, 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 the discussion of 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's take an explanation of an operator 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 the time I3 = 5; the plus number implements two arithmetic 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 number implements two characters String is added to the calculation function. 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); / * Calls when creating this object is a constructor with parameters * / flashmagic = new Employee (); / / Creating this object is the result of the call that the value-value constructor is obtained? 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 function, virtual functions, template and other European discussions, because Java laxant Meng smashed surname 庑 ┝ ┝ 2   枰   嵝 阋 阋   贑 贑 贑                                            In , the functions defined in the class are 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! 2.3.3 Initialization and assignment here I have to remind you that initialization and assignment are completely different concepts. When you create a class, you call this class constructor to initialize the properties of the object. And if this object is given to other similar types of objects, it will be less simple. The value is directly assigned directly in Java, because the pointer is canceled in Java, and there is no deep copy of the pointer to the front copy issue. The copy constructor is required and the operator is overloaded in C . Because Java does not involve these things, I don't have excessive introduction. For details, please refer to the relevant books! 2.3.4 Additional functions: Java no longer supports pointers, so you don't feel its importance because the system will automatically release memory. Everything in C is manual. In the constructor, New has a pointer, and you will be delete this pointer in the paced function. 2.3.5 Static: Now let's take a look at "Static" is something! State a variable or function as static requires "static" keyword. Declare static purpose is to "a single storage space for all objects of a class or method". Static data is classified, not any object. Static data is in the statement that the system allocates memory space without waiting for the object. For example to help you better understand it. Or follow the example above. I still remember the employees I just said able to use the microwave oven. Now we have to find a glove, after all, I want to take a hot meal from the microwave oven directly to pick it up. I define my glove into a Boolean variable, which has two states of clean and dirty. Think about who is the glove? All objects? wrong! Because only methods can belong to all objects. It belongs to the class. Like the method of the microwave oven, there is only one in memory, and all objects can be modified by the method. The next revision is based on the previous revision! I mean: an employee stains the gloves, and the next employee is still dirty. And after this employee was cleaned, it was clean when others used it again! Just do things, understand! I don't think much about the static function. I feel that it is a class, and the memory is allocated when defined. Call is to call directly using the class name.

Other and ordinary member functions have no difference, but here you need to explain that in Java, static member functions can only modify static properties, and static properties can be modified by all member functions. But there is not so much thing in C ! 2.4 Inheritance Inherit is very good, its greatest benefit is "code reuse", which greatly improves work efficiency. You will understand for an example. Black and white TV in the world, it has its own principle. However, people develop on his basis.

Out of a color TV. Color TV inherits all the features and methods of black and white TV! Because it can display a color image or black and white images. However, there are many differences from black and white TVs in the principle of working. Color TV and multiple matrix circuits, separating color signals (RGB), and he can display colorful images. The black and white TV does not have this circuit, even if it receives a color signal, it does not appear a color image. Color TV is derived from black and white TV. Therefore, the black and white TV is a parent class, and the color TV is both a subclass, a color TV inherits all the characteristics and methods of black and white TV. Look at what it is like again in the computer:

//Bwtv.java definition of parent class bwtv {private int A; public bwtv () {a = 1;}

Public changebWTV (INT i) {a = i;}}} //ctv.java subclass definition Class CTV EXNTENDS BWTV {// Note Keyword "Extends" Private Int B; public CTV () {b = 2;} public ChangeTCV (INT X) {b = x;}}

With the definition above, let's see what data they have. BWTV data includes CTV data including Private Int a private int a private int b public changebwtv (); public changebwtv () public changectv (); You see, subclasses have all methods and properties of the parent class. Pay attention to the keyword "extends", it means inheritance. Use ":" operator in C . It is the same. But there are many problems here, first is the problem of access rights, and the subjects of the subclass have all the properties and methods of the parent class. Yeah? It is definitely right! (However, Java's book is not said so. He said that only the attributes and methods of non-private types, I think it has a mistake!) However, the object of the subclass cannot directly access the parent class private property or method. It can only be accessed by the public member function of the parent class. At this time, if you modify the value of the parent class's properties. That is really modified. I mean: The value of the private attribute of the parent class will change as the sub-object calls the public method of the parent class to change the changes to the corresponding attribute! (There is a problem with a domain, all modifications are made in subclasses, modifying the properties of the parent class inherited by subclasses (in subclavab, at this time, the parent class is copied into the subclass .).). The properties of the parent class defined in the program do not change any changes (in the field of parent class), then the constructor is the constructor, first call the constructor of the parent class when creating a sub-class object. Then, then call the structure function of the subclass, after all, the constructor of the subclass does not include the initialization function of the attribute of the parent class! (From this point, my point of view is the correct "subclass of objects with all the properties and methods of the parent class") Of course, the call sequence of the quotient functions is just the opposite! Let us now talk about Protected this keyword, it means: For objects, the variable that protected for protected is private, and the sub-parent class, the variable declares that protected is public. There is a problem now, if we also define an int type variable A in subclasses, then call the child class definition or parent class definition when you create a subclass? This involves the hidden problem of data, I can tell you that it is definitely the variable A of the subclass of the call. Because the subclasses hide the same name variable of the parent class. And if it is a way? This involves the problem of reconstruction. I mentioned above "" Function Reconstruction refers to the method of declaring the same name as the parent class in subclasses, thus covering the method of the parent class. Reconstructing the subclass and the father The difference problem of class. "Here, you must declare that in Java, the subcatencies have a coverage of the hidden and parent class methods for the parent class properties, in the subclass, the subclass object can only call the sub-class itself. Attributes and methods. To call the properties and methods of the parent class, you must use this key. And in C is not like this. Because it has virtual function virtual functions in C very fun things. We can declare the functions that need to be rewritten as virtual functions, and declare this keyword with Virtual. such. If we cwinApp such a base class, it defines a member (virtual) function for initInstance () and another (virtual) function initApplication (). If I derive a sub-class from CWINAPP as CMYWINAPP and modify the INITINSTANCE ().

转载请注明原文地址:https://www.9cbs.com/read-19328.html

New Post(0)