Students who have studied C++ or Java must have understood object-oriented content. Programming languages are generally divided into two design methods: object-oriented and process-oriented . Early programming languages were mostly process-oriented and were composed of multiple processes. Python was designed as an object-oriented language, so Python is an object-oriented programming language. In this section, we first learn about the concept of object-oriented programming.
First of all, we need to learn what an object is. The English word for object is 'Object'. There are objects everywhere in our lives. The computer in front of you, the mobile phone in your hand, or the books on the bookshelf are all objects. Objects are a kind of object. An actual existing entity.
When we learn objects, we can think of objects as two parts, one part is called attributes , and the other part is called behavior .
For example: We bought an alarm clock. The material of the alarm clock is its attribute, and when the alarm clock sounds, it is its behavior.
The data types we have studied before all belong to classes. Classes are the basic structure of Python. Classes contain many methods. When we learn classes, we can understand classes as a classroom. When we create a classroom, we It is stipulated that this classroom needs to learn Chinese, mathematics and English. When a student joins this class, he needs to learn these three subjects. Classes are often a carrier of our definition, and members in a class will have the same attributes and behaviors.
We can think of all cars as a class, or we can think of all airplanes as a class. A class is a relatively abstract concept, and we must define it according to the actual situation when defining it.
Object-oriented programming has three major characteristics: encapsulation, inheritance and polymorphism.
Regarding packaging, we can learn from examples in life. First, let’s think about it literally. Packaging is to seal things up. When we buy a speaker, there are circuit boards, circuits and other structures inside the speaker, and We users do not need to know its internal structure and usage principles, so these structures are enclosed inside the box, and some interfaces are provided outside the box for us to connect. These things inside the speaker are encapsulated.
We can also use the concept of encapsulation when writing programs. For some content, we do not provide interfaces to use them. They are internal structures and immutable content. This content is called encapsulation.
When it comes to inheritance, everyone usually thinks of inheritance. If we use inheritance to describe inheritance, inheritance in real life is usually one-to-one. If it is one-to-many, then the inheritance obtained by multiple people will be very large. It may be inconsistent, and our inheritance in the program is a complete inheritance relationship.
We can think of a car as an object, and whether it is a Mercedes-Benz, a BMW, or a Volkswagen, these cars inherit all the attributes and behaviors of the car. We regard the car as a class and define the attributes of driving on the ground and 4 tires for the car. Then when the BMW inherits this class, the BMW car obtains the attributes of driving on the ground and 4 tires. If we give the car The behavior of high-speed driving is defined, and then the BMW car also inherits its high-speed driving behavior.
When we use the inheritance relationship, we call the car a 'super class' or 'parent class', and the inherited BMW car is called a 'subclass' or 'derived class'.
Polymorphism means that a parent class has multiple attributes, and multiple subclasses are derived based on these attributes. Let's continue to take the car as an example. We can define two attributes for the car, one for driving on the road and one for driving on the track. And both of these attributes include being able to carry people, then we are When defining the inheritance of a subclass, you can define two subclasses, one is a car that can carry people but drives on the road, and the other is a car that can carry people but is on a track. This kind of class is derived The way in which multiple subclasses have public and private properties is called polymorphism.
Regarding the two concepts of classes and objects, they are very important when we learn programming. No matter in any computer industry, the use of classes and objects must be inseparable. Use more examples in life to think about what classes and objects are. , in the next section we will learn the definition and use of classes.