Basic concepts of object-oriented programming
Basic concepts of object-oriented programming
The main idea of object-oriented programming (Object Oriented Programming, OOP, object-oriented programming) is to decompose the various transactions that constitute the problem into individual objects , the purpose of establishing an object is not to complete a step, but to describe the behavior of a thing in the entire problem-solving step.
Process-oriented means analyzing the steps required to solve the problem, and then using functions to implement them step by step, and then calling them in sequence.
Object-oriented and process-oriented are two different programming ideas. No one method is absolutely perfect and needs to be determined according to the specific project. For example, to develop a small software or web page, the project amount is small and can be completed in a short time. You can completely adopt a process-oriented development method. Using object-oriented method will increase the amount of code and slow down the operating efficiency.
Process-oriented programming languages (such as C language) cannot create classes and objects, and cannot use object-oriented methods to develop programs; object-oriented programming languages (such as Java, C++, PHP) retain Process-oriented keywords and statements can be used to develop programs in a process-oriented manner.
Object-oriented programming was proposed after process-oriented programming.
We don’t have to pursue conceptual understanding. Many experienced programmers can’t even fully explain the difference between object-oriented and process-oriented. We must focus on practice and deeply understand the ideas of programming languages.
Basic concepts of classes and objects
For the convenience of explanation, we will start with real-life examples.
We know that the castings used in industry (pots for cooking at home, car chassis, engine fuselage, etc.) are all cast from molds, and one mold can cast many identical castings. , different castings can be cast from different molds.The mold here is what we call "class", and the casting is what we call "object".
A class is a template for creating objects. A class can create multiple identical objects; an object is an instance of a class and is created according to the rules of the class.
Properties and methods
The casting (object) cast from the mold has many parameters (length, width, height, etc.) and can complete different operations (cooking, bearing weight, protecting internal parts, etc.). The parameters here are the "properties" of the object, and the completed operations are the "methods" of the object.
Attribute is a variable used to represent the characteristics of an object, such as color, size, weight, etc.; method is a function used to represent the operation of the object, such as running, breathing, jumping, etc.
The properties and methods of an object are collectively called members of the object.
Inheritance of classes
A class can inherit the characteristics of another class, just like a son inherits his father’s DNA, personality, property, etc., which is similar to that in real life Inheritance is similar.