Home  >  Article  >  Basic characteristics of object-oriented programming

Basic characteristics of object-oriented programming

(*-*)浩
(*-*)浩Original
2019-07-25 10:35:0831877browse

Object-oriented programming(Object Oriented Programming,OOP) method is currently a popular programming method. Compared with process-oriented programming, it is more in line with human beings. Natural way of thinking.

Basic characteristics of object-oriented programming

#The basic characteristics of object-oriented programming are: encapsulation, inheritance, and polymorphism. (Recommended learning: PHP video tutorial)

1. Encapsulation

Encapsulation is to hide the properties and implementation details of the object, only The externally exposed interface controls the access level for reading and modifying attributes in the program, and combines the abstracted data and behaviors (or functions) to form an organic whole, that is, the data is organically combined with the source code for operating the data. Combined to form a "class", data and functions are members of the class.

The purpose of encapsulation is to enhance security and simplify programming. Users do not need to know the specific implementation details, but only need to use the members of the class with specific access rights through the external interface.

Isn’t object-oriented the object-centered analysis when using programs to handle things? It is different from process-oriented, which cares about the logic, flow and other issues of processing, but does not care about the subject of the event. Object-oriented means subject-oriented, so when we solve problems, we should first encapsulate objects (objects are instances of encapsulated classes, for example, Zhang San is a person, and person is an encapsulated class. Zhang San is just an instance and an object in the object. ). For example, the little rabbits and little sheep in our daily life can be encapsulated into a class.

For example, the attributes of a rabbit include two ears, four legs, a pair of eyes, three mouths, etc.; its behaviors (functions) include running, jumping, eating, etc.

2. Inheritance

Inheritance is one of the basic features of object-oriented. The inheritance mechanism allows the creation of hierarchical classes. Inheritance means that a subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance fields and methods of the parent class, or the subclass inherits methods from the parent class, so that the subclass has the same behavior as the parent class. Similar to the picture below:

We have encapsulated the rabbit class above, and other animals can also be encapsulated. During the encapsulation process, we found that the two classes rabbit and sheep have similar functions or characteristics, such as eating grass, so we can extract common features and methods to form a higher-level class, such as herbivores and carnivores here. Inheritance is the relationship between child and parent classes. The inheritance mechanism can well describe the ecology of a class and also improves the code reuse rate. The Object class in Java is the super class of all classes and is often called the God class.

3. Polymorphism

Polymorphism The ability of the same behavior to have multiple different manifestations or forms. It means that the same method of a class instance (object) has different manifestations in different situations. Polymorphism enables objects with different internal structures to share the same external interface. This means that although the specific operations on different objects are different, they (those operations) can be called in the same way through a common class.

Advantages of polymorphism:

1. Eliminate the coupling relationship between types

2. Substitutability

3 . Scalability

4. Interface

5. Flexibility

6. Simplification

Three necessities for the existence of polymorphism Conditions:

Inheritance rewriting (subclass inherits the parent class and redefines the parent class method) The parent class reference points to the subclass object

In short, polymorphism is actually On the basis of inheritance. For example, today we are going to the zoo to visit animals. Then you are right to say that we are going to visit rabbits, sheep, lions, and leopards, but you cannot say that we are going to visit cars. In this example, the subclass is polymorphic: in addition to using its own identity, it can also act as a parent class.

For more PHP related technical articles, please visit the PHP Graphic Tutorial column to learn!

The above is the detailed content of Basic characteristics of object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn