What Is Object-Oriented? Can you give this answer (I believe there is no correct answer )? I think object-oriented is just a tool in software engineering. So what is the purpose of software engineering? The purpose is to compile software products that are reliable, efficient, flexible, scalable, and reusable. Since object-oriented is a tool in software engineering, the purpose of using object-oriented should also be. That is to say, we use object-oriented programming to write reliable, efficient, flexible, scalable, and reusable software products. We must take this purpose as the center to learn and use object-oriented, otherwise you will lose direction. Most of the time we talk about object-oriented, mostly about object-oriented itself, rather than how to use object-oriented to solve practical problems, which can easily mislead beginners. With the addition of gorgeous text, many people think that everything can be OK as long as the object is oriented. For example, give you the best gun and teach you how to use it. After a while, you will be familiar with this gun. Now let you go to the target site. The result is 100 rounds of bullets and none of them are hit. Hey, didn't I use the best gun? Why can't I get hit? Because you don't know the shooting essentials, and you don't know how to target the target. Of course, you can imagine the results. We need OOP (Object-Oriented Programming) instead of POO (Programming Object-Oriented ).
Object-oriented has three features (usually mentioned in Object-Oriented), namely inheritance, encapsulation, and polymorphism. Literally speaking, it is a brand new concept for us, but as long as you analyze it, we will find that these are the concepts we often use in programming.
What is encapsulation? Is encapsulation only available in Object-Oriented Systems? No, as long as the program is encapsulated. Variables and functions are often used as encapsulation. For example, if you write a function to retrieve the user status, the logic for retrieving the user status is closed for the program that calls this function. Therefore, when the logic changes, does not affect the called program. Variables are also encapsulation. You define an int variable. No matter what value you assign to it, it is an integer value for the program that calls this variable. When it comes to encapsulation, you have to mention interfaces because encapsulation is an interface process. What is an interface? Is the interface only when the keyword "interface" is used? The description in the dictionary is the contact surface and interface of an object. The world is composed of objects, so interfaces are everywhere in our lives. You can see a ball. It is a circle, and a circle is an interface. Our programs are also composed of interfaces, data types, keywords, functions, variables, and so on. For example, if you see the word 'int', you will know that it is an integer data type. We can say that 'int' is an integer data type interface. Therefore, encapsulation and interfaces are not exclusive to object-oriented systems, but they expand their scope in object-oriented systems. We often see the discussion between object-oriented and process-oriented on the Internet. In fact, these are not important, because which method is used is an interface process. The key is that programmers who use these methods can see how your abstract capabilities are.
Polymorphism may be one of the most difficult concepts to understand. polymorphism is hard to come across because you have never learned object-oriented. In fact, this is also a concept we often use, but it is not named (at least no polymorphism is used to name it ). Let's look at the example below,
Double a = 0, B = 5, c = 0;
A = 1;
C = a + B;
Cout <c <endl;
A = 3.5f;
C = a + B;
Cout <c endl;
Finally, let's talk about the object. The focus is here ~ First,Class is a module. In the past, our module was a file, such as A. c or. cpp file. There is no limit on what function to write in a module File (Theoretically there is no clear definition of how to write). deploy these function based on the developer's experience. Therefore, it is easy to write a super module (a file contains dozens or hundreds of Functions), and it is inconvenient to maintain the code. Object-oriented solves this problem well. As long as programmers who have read basic object-oriented theories know, they should first define classes and then put relevant functions into classes. There is a single responsibility principle in object-oriented systems. If you follow this principle, you can write out a good module. More importantly, it is easy to maintain communication. For example, the previous communication may be like this. Modify the customer. a function in the cpp file, in object-oriented, can modify a method of the customer class. This is very important because it is closely related to people's thinking. For example, if we go to the restaurant for dinner, you say to the waiter that I want a bottle of beer, I'm sure the waiter will ask you again: Sir, what brand of beer do you want? But if you say: I want a bottle of budway beer, the waiter will never ask you again. If a system is regarded as a production line, class is a worker. One worker is only responsible for one operation, A is responsible for drilling, B is responsible for installing screws, and C is responsible for packaging. This is very efficient, and there is a problem, it is good to determine the worker where the problem occurs. Second,Class is a custom data type. After understanding this, your ideas will be clear and you will not be new to object-oriented. Let's take a look at the following simple example,
The output result is68.5We first define three variables, a, B, and c, assign values 1 and 3.5 to A, and then output the results of a + B. We can see that, the same logic changes with the value, and the output results are also different (the effect of polymorphism ). Here, double is a high type (equivalent to the parent class), and 1, 3.5 (int, float) is a low type (equivalent to a subclass ). This example is variable polymorphism, and there are examples of function polymorphism. Of course, anyone who has written C knows the function pointer, which is the function polymorphism.
// Calculate the combination of two numbers
Double sum (double a, double B );
Int _ tmain (int argc, _ TCHAR * argv [])
{
Double a = 0, B = 0;
A = 1;
B = 2;
Cout <sum (a, B) <endl;
A = 1.2f;
B = 2.3f;
Cout <sum (a, B) <endl;
Return 0;
}
// Calculate the combination of two numbers
Double sum (double a, double B)
{
Return a + B;
}
In this example, the int and float values of the lower type are assigned to the double of the higher type, and the sum function is used to combine them. If the programming language does not support this type of conversion, we have to write two functions,
// Process int Calculation
Int sum (int a, int B );
// Process float computing
Float sum (float a, float B );
We have discussed object-oriented and its three features above. We can see that these are the concepts that are often used in programming. I think the biggest breakthrough is to provide us with a new mindset, a new code structure, and a vague definition (such as polymorphism) in the previous software design ). How can we change our thinking? Think of you as a company manager. You are now the manager of a company. The Department is the subsystem, and the employee is the object. All you need to do is,
-You have to deal with many things every day and be prepared for them.
-What business does the company do?
-How many departments do I need to create ?, Rules for communication between departments.
-What kind of job is required? What are the tasks for each job?
-Which department is required to participate in business processes and business processes? Which employee is required to participate? How do they communicate?
-Learn from the successful management model, but do not use it blindly because it is too costly to comply with the company's situation.
-Do not set too many communication links. It is simple and easy to complete.
-Prepare for changes. There is no right or wrong in the world, and it will always be corrected as we move forward.
That's all? Well, the truth is very simple, but it is very difficult to be a good company, because the variable is too large. The same is true for Object-Oriented systems. Efforts are required to achieve this.
Therefore, we can determine that, among the same type, polymorphism can be achieved only by providing conversion from low to high (I don't know, there is no concept of object-oriented ). Of course, this type of conversion is also required for classes as data types. But the class is a custom type. Therefore, we must define which is a high type and which is a low type: the parent class is of the high type and the subclass is of the low type (Is Inheritance born so :)). Inheritance allows us to reuse the code of the parent class, but in this example we can see that its main purpose is polymorphism.