Python It is an object-oriented language . Object oriented has three major features : encapsulation 、 Inherit 、 polymorphic .
Let's talk about these three characteristics :
1、 encapsulationHide object properties and implementation details , Only public access is provided . stay python Set the property to private by starting with a double underscore .
benefits :
1. Isolate change ;
2. Easy to use ;
3. Improve reusability ;
4. Improve safety .
2、 InheritInheritance is a way to create new classes , stay python in , The new class can inherit one or more parent classes , A parent class can also be called a base class or a superclass , The new class is called a derived class or subclass .
That is, a derived class inherits the fields and methods of the base class . Inheritance also allows the object of a derived class to be treated as a base class object .
for example , There is such a design : One Dog Object of type derived from Animal class , This is a simulation " It's a (is-a)" Relationship .
python The inheritance of classes in is divided into : Single inheritance and multiple inheritance
class ParentClass1: # Define parent class class ParentClass2: # Define parent class class SubClass1(ParentClass1): # Single inheritance , The base class is ParentClass1, Derived classes are SubClassclass SubClass2(ParentClass1,ParentClass2): #python Support for multiple inheritance , Separate multiple inherited classes with commas
3、 polymorphic Many forms of a thing , Function rewriting is actually an embodiment of polymorphism .Python in , Polymorphism means that the reference of the parent class points to the object of the child class .
Steps to achieve polymorphism :
1、 Define new subclasses
2、 Override the corresponding parent class method
3、 Use subclass methods to deal with , Don't call methods of the parent class
The benefits of polymorphism :
(1) Increased program flexibility
(2) Increased program scalability
This is about Python This is the end of the article on the three characteristics of object-oriented . I hope it will be helpful for your study , I also hope you can support the software development network .