Object-oriented programming, my thoughts [on]

xiaoxiao2021-03-06  97

Foreword: The purpose of organizing this information is to help my classmates can understand more intuitive understanding of 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 in this way, let's take a look at the writing format: int AddNum (int x, int y) {return x y;} Specific meaning is this: int / * Return value type * / addnum / * method (Black Box) Name * / (INT X, INT Y / * Parameters * /) {RETURN X Y; / * Interior is a method (implementation added operation,) and returned to the caller 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;} Specific function Description: int myage = 0; // Defining and initializing my age is 0; int A = 20; / * Define variable a equal to 20 * / void / * return value type is a non-return value type * / remote / * function name * / (int A / * Parameters * /) {myage = a; // Internal implementation method, pay attention, no return returns! ! ! } There are a lot of topics about functions. It will be introduced here. My purpose is to let you know how the function is! 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.

.4 operator and control statement: still 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 employee's name Return Name;} // ......, etc., we define our place. The data type required is required. 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); // Shows my age}} The result is: my name is jw This string is the X of the output window The coordinate axis is 10 px, the Y coordinate axis is 10 PX position. 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 of the inside, huh, huh, I will not :) First, let's take a look at our custom data type Employee, it is nothingord of the int type when it is applied, the same needs Create a variable (object), but the former is defined by himself, 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, of course, there is no need for C . Regarding their details of the details, refer to the relevant books. This is immediately settled, and it will have practical significance to follow this object of jingwei. 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 them 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 :) Quote from: http://www.vikosoft.net/trackback.asp? TBID = 522

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

New Post(0)