Object-oriented programming, my thoughts

xiaoxiao2021-03-06  90

Preface: The purpose of organizing this information is to help friends can more intuitively understand the object-oriented programming. Let later people can take some detours. However, there is a lot of vulnerabilities and mistakes, but also ask the seniors to make valuable changes, after all, exchange will make us constantly progress. The technology is a sun-changing, he will not wait for your growth. Technologies should take out to exchange in others, they learn to understand their own subjective consciousness, there is a wrong mistake! Communication will make progress faster. I think if the computer's architecture does not have a revolutionary change, the program language we applied now is not too depreciated! What is the programming? thought! Proficient in a programming language (preferably object-oriented language), then go to other programming languages, you will find that the process is such a row of clouds! why? You have already mastered the idea of ​​programming, then learn other, nothing more than learning a new syntax format. I am not here to discuss how to use C or Java, nor and how to learn how you discussed them, I want to discuss with you how to understand the object-oriented. It will mainly involve "class, object, inheritance, attribute, method, static, overload, hidden, refactoring, statement, definition, initialization, assignment, etc." I will only pass by, let you have one What is the meaning of this tour means that I have achieved the purpose, and more detailed technology inside, please refer to other related books and go deep into the study! Because I just discuss how to better understand the object! How to improve efficiency? Repeat the resources and use others to use. This is a very good idea! For you, the biggest resource is confidence and enthusiasm! Ok, let's make a spirit, let us go to the object-oriented programming! Note: All programs in the article I use Java, of course, in C , it is very small, and I will point out in different places! Note: The text of the text in the article uses black, indicating that the text is blue, emphasizing the text with orange, and the text is used to use red!

text:

1. Basic Concept: 1.1 Class and Objective To tell me, whether it is a process-oriented language, the object-oriented language, I must first give him a class and object! -------- "What is the composition of this world?" This question will give different answers if the different people will answer. If it is a chemist, he may tell you "Is it still useful? This world is a chemical substance of molecules, atoms, ions, and the like." If is a painter? He may tell you, "This world is composed of different colors." ... huh, huh, everyone is rumored! But if you let a classified person to consider the problem, it is more interesting, he will tell you that "this world is made by different types and things" good! " As an object-oriented programmer, we have to stand at the perspective of a classified perspective! Yes, this world is made of animals, plants, etc. Animals are divided into single cell animals, multi-cell animals, mammals, etc., mammals are divided into people, elephants, tigers ... I will go down! Now, stand in an abstract angle, we give "classes" next definition! I mean, standing in an abstract angle, you answer me "What is human?" Let us look at some of the features of human beings, including attributes (some parameters, values) and methods (some behavior, he What can you do!). Everyone has some attributes such as height, weight, age, blood type, etc. People will work, people will walk upright, people will use their own mind to create tools! People can distinguish other types of animals because everyone has the properties and methods of the population. "Human" is just an abstract concept, it is just a concept, it is an entity that does not exist! But all the objects and methods of "human" group are called people! This object "people" are actual entities! Everyone is an object of this group. Why is the tiger not? Because it does not have the properties and methods of this group, the tiger will not walk upright, will not use tools, etc.! So tell the tiger is not a person! This shows that ------ class describes a set of objects with the same characteristic (attribute) and the same behavior (method). In the program, the class is actually the data type! For example: integer, decimal, etc. Integral also has a set of features and behaviors. The difference between the language-oriented language and the surface object is that the process-oriented language does not allow programmers to define data types themselves, and can only use the built-in data types in the program! In order to simulate the real world, in order to better solve the problem, we often need to create the data type necessary to solve the problem! Object-oriented programming provides us with solutions.

1.2 Built-in Data Type and Function: Computer programs must be tracked with 3 basic properties when storing data: 1. Where is information stored; 2. What is stored? What is the type of information stored; let's take a look at what is the built-in data type of the programming language! (Oh, this is not good to say, because every language has its own unique data type, but this is a few, such as there is a Byte type data in Java, and there is no in C , I hope you can give one more!) Integer "int", floating point type of data "float"! String "string", and arrays also have structures, and so on. However, when writing the program, we will create a type of variable or constant as needed, for example, because we need to create a shaped variable i is 5, we can do this, int i = 5; and I am very It may change the value of i, which is to assign a value from the new to it, such as letting it wait and 6, you can change it in the required place to change to i = 6; thereby, we know that the amount of change can occur on "value" variable. The amount that does not change will be called constant, with count keywords in C , and in Java, use Final keywords to declare. Due to different languages ​​in different languages, don't be introduced here, detailed content clearing through books! Here we mainly discuss the function, we can imagine the function as a "black box that implements a specific function" ----- This feature is set by you, for example, for example, now I ask How much "2 3 is equal to?" I believe that you can answer me soon is equal to 5. Let's analyze what information is included in this sentence! First of all, I want to imagine your brain into a black box. I don't know if it is not necessary to know how your brain is working (how is it to operate), what information I did to give you? What processing do you have to do? What information is you returning to me? Need to remind you that every method will return a message to the caller, except for the constructor (later I will make a detailed introduction). I need to take myself as a programmer now, and you? Of course it is a computer! Calculation can be so smart, it will only be run according to a well-known specific format, I want it to have the functions as described above, I will define this black box! First of all, I want to tell this black box will have two integer values ​​to you (this is the so-called parameters, the programmer needs to give the black box), then define the two integers to implement the two integers inside the black box (this is The machining made by the black box on the data can do any processing as needed.). Finally, I will be marked it back to give me a value of integer (this is the information of the black box to the programmer). A function is defined so, let's take a look at the writing format: int AddNum (int x, int y) {return x y;}

The specific meaning is like this:

INT / * Return value type * / addnum / * method (black box) name * / (int x, int y / * incoming parameters * /) {RETURN X Y; / * is an internal idea method (implement additional operation , Return to the caller result with return * /}

First, please pay attention to the "return" statement! The meaning of the return key is to return to the caller to follow the information behind it! Just like above, because I ask you, you will answer me, if I don't ask you, you don't have to answer me! Like the computer, where is the function to call? I can only tell you, where is it to call! Of course, you can change the parameters, return values, and internal implementation as needed, and how to define how to call you had to refer to the relevant information! Here I am just give you a thought! Sometimes you will encounter such problems, I let you remember, my age is 20 years old! Understand from the literal, have you returned to me! However, in fact, you really return information, the content of the information is "no information, that is, there is no return value type void". The specific procedures are as follows: int myage = 0; int A = 20; Void Remage (INT A) {myage = a;}

The specific function is described below:

INT myage = 0; // definition and initialize my age is 0; int A = 20; / * Define variable A equal to 20 * / void / * Return value type is non-return value type * / remote / * function name * / (INT A / * Parameters * /) {myage = a; // Internal implementation method, pay attention, no return returns! ! ! }

There are still a lot of topics about functions. It will be introduced here. My purpose is to let you know how the function will be! Paper for the following discussion!

1.3 Pointers and references: pointers and references are in C , and there is no java. The operation of the memory is canceled in Java, and the operation of the operator overload is also canceled. However, I will still introduce some operator overload functions. The reference is mainly used on the transfer of function parameters. So I don't have too much introduction here. They are very practical, interested in classmates can refer to C related books. 1.4 Operators and control statements: or look at the relevant books, this is no longer mentioned here!

2. In-depth discussion of object-oriented: 2.1 "Types" internal details: With the above knowledge, we can now implement the internal implementation of the in-depth mining class. All knowledge points I will start with the object, before this, I hope that you can confirm that the basic content introduced to the above is completely mastered! Yes, the biggest feature of object-oriented programming language is to write your own data type to better solve problems. I think I have to help you engage in the relationship between class, object, attribute, method! As I said earlier, the "class" is not doing, because "human beings" is just an abstract concept, it is not a real "thing", and this "thing" is the so-called object. Only people "objects" can work. And what? Class is a description of the object! Objects are generated from the class! At this point, the object has all the properties and methods described by the class. ------- Be sure to understand this sentence! ! ! Maybe you have already somewhat, it's okay! Good aftertaste, I will give an example! For example, televisions, TV sets have a working schematic, then what is TV? As long as it enables the object of all the features of the working principle, we call it TV. Do you think about this? However, the schematic of the TV is not working, that is, this schematic cannot watch the show, only the TV "entity - the so-called object" can watch the show, that is, after the class generates an object, Is true meaning! Can you start working. At this point, the TV has all the properties and methods described by TV schematic! Understand, huh, huh! I have previously introduced that the class is a collection of attributes and methods. These attributes and methods can be declared as private, public or protected, which describes access control to class members. Below I will do this, respectively: 1. Public (public): After the variable is declared as a common type, then you can directly access the object, everything is exposed! That is, your credit card password is also able to get directly. 2. Private (Private): If you declare the variable as a private situation, you will get my credit card password, and the object must call a dedicated method to get. 3. Protected (protected): Introduce the inheritance discussed. 4. Default Control Access (Friendly): // Nothing in C in Java. In order to achieve data package, improve data security, we generally declare the class's attributes as private, and declare the method of the class as public. Thus, the object can directly call all the methods defined in the class, and you must call to define a good dedicated method when you want to modify or get your own properties.

You think, will you publish your credit card password? Ha ha! Therefore, we advocate: "Object Mode, Method Reform Attribute"; 2.2 View memory allocation through example: So much, let us look at an example! For example: Now we have to write a company employee management system, what do you think is the most suitable data type? I think it is an employee! However, in the process-oriented language, this is not allowed because it can only use the internal data type in the language! And employees are not within this internal data type! Maybe someone will say that you can use the Struct in the C language! After all, it is the foundation of the class! If you are previously a programmer of C or B, please forget these, let's take a look at how to use classes to achieve this! An employee of a company is a special group of human beings. In addition to all of the characteristics and methods of humanity, it has additional features and methods, such as her salary, credit card password, schedule, etc., these features and Work content, workload, etc. These methods. And how should we define this class in your computer? Below I will write its format, let you see what it looks like in your computer! / * Here I need to declare again, I use the Java format, which is very different from C on the grammatical format! Many details and internal operations have a lot of differences, and doing small differences in thinking * //employee.javapublic coployee {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 // ... Waiting for public void heater () {// This method is used to process lunch lunch = true;} public void setname (String a) {// This method is to modify the name of the employee = a;} public string getname () {// This method is to get the name of the employee Return Name;} // ......, etc.

This way we define the data type we need. Now let's take a look at what it can do and how to work! What I want to do is that there is a light rod commander in the studio, "jingwei", I modified its name and output it, see how I did it! Note: Please observe how the object calls the method, it uses "." Operator! In fact, this is the case when the object calls a common attribute or method. However, in C , if a similar type of pointer is defined, the "->" operator is used when the pointer is called. More detailed content clear refers to the relevant books!

//WorkStation.java Import Java.awt.graphHics; Import Java.applet.applet; public class workstation extends applet {private employee jingwei; // Object declaration, not allocated memory! Public void init () {jingwei = new Employee (); / * At this point, create an object call constructor, introduce * / jingwei.setname ("jw"); // Set my name} public void Paint (Graphics g) {g.drawstring ("My Age IS" jingwei.getname (), 10, 10); // Show my age}} The output is:

My Name is JW

This string is a position where the X coordinate axis of the output window is 10 PX, the Y coordinate axis is 10 PX. What I have to do now is to make a big anlyiaque to make the above program, let you see how it is, how is it! (I will take you to the compilation inside, huh, I will not :)

Let's take a look at our custom data type Employee. When it is applied, it doesn't have something different, the same needs to create variables (objects), but the former is defined, and then this is its built-in . There are many properties in this class, there are many ways. At this time, we cannot call its properties directly with the object we have created. Because it is Private Protection Type! I want to modify my name, I want to call the setName () method with the object, and I want to get my name to call GetName () this method. We have been walking according to the route, this route is "object adjustment method, method change attributes", I really believe that you have understood how this is a thing! Ha ha! Leading up the sails and continue! Let's take a look at Workstation class now. This is a master class, and the taste of the main () function in C is similar. Among them, in Java, a file only allows for it and must have a primary class, this main class is declared by public! He must have a main () function in C is the same. Let's take a look at the first statement in this class! Private Employee Jingwei; This statement is to declare an Employee object JINGWEI (no declaration in C ). I want to tell you the difference between "declaration" and "definition". The statement just tells the computer that there is such a variable (object), which does not assign memory for this variable (object) in memory! And only the variable (object) will be assigned memory only when defined. (Required, the init () method is to complete the initialization operation, define the object here, assign a memory. The Start () method is used to initiate the main thread of the browser, the Paint () method to display the Apple interface. These are what the Applet program is required. As for the Application program, it is not required. Of course, it does not need them in C . About their details clear refer to the relevant books to start an object, for jingwei this object The operation will have practical significance. Don't have this idea: "Try to operate the class!" Just as I said in front, the principle of TV can't watch TV! This is meaningless! Look at this statement jingwei = new Employee (); it means to define an object of an Employee type. At this point, I want to tell you: "Jingwei this is what you want to have." It has all the properties and methods described in the class. Let me list it one by one:

/ * All Employee objects have these properties. These properties of the corresponding object are stored for each of the newly assigned a single memory. I mean that every object has its own "unique" one * / 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 / * All EMPLOYEE objects have these methods. But only one in memory * / public void heater () {// This method is to process the lunch lunch = true;} public void setname (String a) {// This method is to modify the employee's name name = a;} public string getName () {// This method is to get the employee's name return name;} / * However, when you create this object, the computer only assigns memory only to all properties of this object, but no Assign memory to the method. The method only one is that all objects are all objects, so no matter how many objects have been created, the computer will only assign a memory for a method. * / I think I still give an example, otherwise you are not marked. Ha ha! See my statement "Private Boolean Lunch;" The company does not care about lunch, each employee needs to take a meal. We think now, the company's space is all memory capacity, your work is part of the memory in the computer (one of each employee is assigned when creating objects). You bring lunch to the company, put on your desk. "Lunch" occupies a corner of your desk (accounting for a memory capacity of your own "object"). This lunch only belongs to yourself, the same others only belong to her! So each employee (object) requires a fast space (memory) to store your own lunch (attribute). This is also the case in the computer. Each time you create an object, you will place a "lunch -lunch" attribute (all properties owned by the object) in memory. Computers will only assign memory to objects' properties. Because every object is different! Just like lunch taking lunch and giving lunch that I have taught in the company is not the same! But the method is different. The rice belt is cold at noon, you need to use a microwave to heat it. The microwave oven can not bring you, the company has (only a space of the company), it is placed on the lunch table. Do you think, who is the microwave? It belongs to all employees! Because each employee can use it. Not having to bring a part of each employee. It can be seen that each employee (object) has a lunch (attribute), but all employees (objects) are only a microwave (method). All employees (objects) can change their lunch (attributes) in the hot and hot state of this microwave (method). Collection! This is the case in the computer, the method only one, for all objects! And attributes are one per object because each object is different. Don't tell me, you still don't understand, or I will hit the wall, huh, huh :)

2.3 In-depth discussion of functions: 2.3.1 Construction function, default constructor, default constructor for the above instance, it is already able to do most of the work, but it is not perfect, there are many details, etc. to be perfect! 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! (Give the "initial value" we want while creating an object)

2.3.2 Heavy Duty Construction Function: 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 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.};

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); / * 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 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 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 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 parent class definition 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 "Extend" Private INT B; Public CTV () {b = 2; PUBLIC CHANGETCV (INT X) {b = x;}} With the above definition, let's take a look at them. What data is there. BWTV data includes CTV data 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 point: PAPP-> InitInstance () // Pointer Call is the false method of subclass CMYWINAPP PAPP-> initApplication () // Pick-up CWINAPP

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

New Post(0)