Object-oriented programming, my thoughts (bottom)

zhaozj2021-02-16  47

2.3 In-depth exploration 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 public (public) type

4, you can overload the constructor.

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 to define the constructor, not the default constructor of the system)

Still look at an example! This is more clear!

//employee.java

PUBLIC CLASS EMPLOYEEEE {

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

//……and many more

Public Employee () {/ This is "default" constructor

Name = "jw"; // Set the employee name

AGE = 20; // Set employee age

SEX = "m"; // Setting employee gender

Emolument = 100; // Set employee salary

Lunch = false; // Set employee lunch

}

Public void heater () {// This method is to process the lunch for processing employees

Lunch = true;

}

//……and many more

}

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 EMPLOYEEEE {

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

//……and many more

Public Employee (String N, Int A, Char S, Float E, Boolean L) {// Look at this constructor

Name = n; // Set the 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 for processing employees

Lunch = true;

}

//……and many more

}

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 to

Jingwei = New Employee ("Jingwei", 20, 'M', 100, FALSE); this, all work is completed, huh, huh! (Give the "initial value" we want while creating an object)

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 point i3 = 5; the plus number implements two numbers 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 EMPLOYEEEE {

Public Employee (String N, Int A, Char S, Float E, Boolean L) {// Look at this constructor

Name = n; // Set the employee name

AGE = a; // Set employee age

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 a parameter

Name = "jw";

AGE = 20;

SEX = 'W';

Emolument = 99;

Lunch = TRUE

}

//……and many more

}

Look, there are two names of the same name in a class, how can we know how we call this version of the function when we use? 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 Calling is the constructor of the value

What is the result? 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!

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 Analytic functions:

The pointer is no longer supported in Java, so you can't feel its importance because the system will automatically release memory for you. 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 a matter!

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 have developed a color TV on his basis. 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

Public 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 data for CTV including

Private Int a private Int a

Private Int B

Public changebwtv (); public changebwtv ()

Public changectv ();

You see, the subclasses have all the 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 functions

Virtual functions are very fun things in C . 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 (). We did not modify the INitApplication () member function. Now we create a function of this class of CMYWINAPP, we create a pointer * PAPP points to this object theApp. at this time:

PAPP-> InitInstance () // Pointer Call is the virtual method of subclass CMYWINAPP

PAPP-> initApplication () // Pick-up CM CWINAPP

Because the subclass does not modify the parent class, the false method of the parent class is called. This involves the problem of the virtual you. It is not discussed here in the position of this article! The type of conversion problem with the object of the parent class and subclass is such that the subclass object is converted to a parent class object, and there will be no errors. Because the subclass contains all the properties and methods of the parent class, it is hard to say when the parent class is converted to the subclass, huh, huh. This will also involve the problem of virtual table, nor discuss it!

Multiple inheritances are no longer supported in Java, which is inherited from more than two classes, but it has more interface concept "interfab". I don't have more introductions here!

There is nothing difficult to abstract base categories! One of his big profile is: to do a multi-class parent class, do not define objects, only derived!

I can do it too. If you can understand the six or seven points of the above, it is the biggest return to me, huh, huh! Just like I just started me, I just give you a probably thoughtful thinking, so that you still need your continuity efforts. There are still many things about programming languages, and it is a small personal ability to be limited and cannot be taken care of. But as an initiator, these things are basically. Need me to remind you, don't expect to understand what is first, two times! Come on :)

04.4.29 Han Jingwei

I am willing to keep in touch with you. If you can make valuable comments and suggestions for this article, I will not be grateful. My email address is:

ONEGENIUS@126.com

Or give me a message in QQ (86228551 --- garbled soul .h)

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

New Post(0)