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 concepts:
1.1 Preliminary prices of classes and objects
I want to say that whether it is a process-oriented language, it is also an object-oriented language, I should 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 the value stored?
3. What type is 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 so defined, 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 / * Parameter * /) {
RETURN X Y; / * The internal is the method (implement additional operation,) and returns 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, how to define how to change the mighty to slow down, ≡ 谡饫 谡饫 皇 皇 皇 歉 歉 阋 鏊 鏊 耄 耄 耄 皇 皇 歉 歉 阋 阋 阋 阋 阋 鏊 阋 鏊 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 阋 Span>
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; // Defines and initializes my age of 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 / * incoming 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 pointer and reference:
The pointer and reference are in C , and there is no in 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 awkward here!
2. In-depth explore object-oriented:
2.1 "Type" internal details:
With the above knowledge, we can now implement the internal implementation of deep excavation classes. 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. Let me do it separately:
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 that "object adjustment method, method reform attribute"; 2.2 Confirmation allocation by example:
Said so much, let's take a look! 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, but in terms of thinking.
//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 void heater () {// This method is to process the lunch for processing employees
Lunch = true;
}
Public void setname (String a) {// This method is to modify the name of the employee
Name = a;
}
Public string getName () {// This method is to get the employee's name
Return Name;
}
//……and many more
}
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.graphics;
IMPORT JAVA.Applet.Applet;
Public class workStation Extends applet {private Employee jingwei; // Object declaration, not allocated memory!
Public void init () {
Jingwei = new Employee (); / * Create an object at this time calling the constructor, later introduced * /
Jingwei.setname (癹 w "); // Set my name
} Public void Paint (Graphics G) {g.drawstring ("My Age IS" jingwei.getname (), 10, 10); // Show my age
}} The output result is: My Name IS JW This string is the position of the X coordinate axis of the output window to 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 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, 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 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; // member ば?? / Span> 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. * /