程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pythons three object-oriented features: encapsulation, inheritance, and polymorphism

編輯:Python

Python It is an object-oriented language . Object oriented has three major features : encapsulation 、 Inherit 、 polymorphic .

Let's talk about these three characteristics :

1、 encapsulation

Hide 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、 Inherit

Inheritance 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 .



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved