What are the three major features of Python object-oriented?

  Programmers who have been exposed to the Python language must know that Python is an object-oriented programming language, so what are its characteristics? The following are the details:

  Python is an object-oriented language. Object-oriented has three major characteristics: encapsulation, inheritance, and polymorphism.

  1. Packaging

  The properties and implementation details of the object are hidden, and only public access methods are provided. In python, the property is set to private by starting with a double underscore.

  Benefits: 1. Isolate changes; 2. Ease of use; 3. Improve reusability; 4. Improve security.

  2. Inheritance

  Inheritance is a way to create a new class. In python, a newly created class can inherit one or more parent classes. The parent class can also be called a base class or a super class, and the newly created class is called a derived class or a subclass. That is, a derived class inherits the fields and methods of the base class. Inheritance also allows an object of a derived class to be treated as an object of a base class. For example, there is such a design: an object of type Dog is derived from the Animal class, which simulates the "is-a" relationship.

  Class inheritance in Python is divided into single inheritance and multiple inheritance

  3. Polymorphism

  There are multiple manifestations of a thing, and the rewriting of functions is actually a manifestation of polymorphism. In Python, polymorphism refers to the reference of the parent class to the object of the subclass.

  Steps to achieve polymorphism:

  1. Define a new subclass

  2. Rewrite the corresponding parent class method

  3. Use the method of the subclass to process directly, without calling the method of the parent class

  Benefits of polymorphism:

  (1) Increased program flexibility

  (2) Increased program scalability

Guess you like

Origin blog.csdn.net/oldboyedu1/article/details/131521010