Python Learning Summary 5: Encapsulation, inheritance, polymorphism

Source: Internet
Author: User

The classes in object-oriented programming have three main features: inheritance , encapsulation , polymorphism

inheritance : creating specialized class objects based on common classes

Encapsulation : working details of hidden objects in the external world

polymorphic: You can use the same action for objects of different classes

The basic form of a class's inheritance definition in Python is as follows:

# Parent Class class superclassname:    block# Subclass  class Subclassname (superclassname ):    block

Description: When defining a class, you can immediately follow the class name with a pair of parentheses, specifying the inherited parent class in parentheses, and if there are more than one parent class, separate the names of the parent classes with commas.

classUniversitymember: # defines a parent class Universitymember     def __init__(self,name,age): Self.name=name Self.age= AgedefGetName (self):returnSelf.namedefgetage (self):returnSelf.ageclassStudent (Universitymember): # defines a subclass  Student      def __init__(Self,name,age,sno,mark): Universitymember.__init__(Self,name,age)# Note To display the call to the parent class constructor method, and pass the argument selfSelf.sno =Sno # Student class has sno,mark two properties Self.mark=MarkdefGetsno (self):returnSelf.snodefGetmark (self):returnSelf.markclassTeacher (Universitymember): # defines a subclass Teacher       def __init__(self,name,age,tno,salary): Universitymember.__init__(self,name,age) Self.tno=TNO #  Teacher class has tno,salary two properties  self.salary=SalarydefGettno (self):returnSelf.tnodefgetsalary (self):returnSelf.salary

As you can see from the code above:

1) in Python, if the parent class and subclass both redefine the constructor method __init () __, when the subclass is instantiated, the constructor of the subclass does not automatically invoke the constructor of the parent class, and the call must be displayed in the subclass.

2) If you need to call a method of a parent class in a subclass, you need to use the parent class name. This method is called in this way, and when called in this way, note that the self parameter is passed in the past.

For an inheritance relationship, the subclass inherits all of the public properties and methods of the parent class, can be called through the parent class name in the subclass, and for private properties and methods, the subclass is not inherited, so it cannot be accessed by the parent class name in the subclass.

Python Learning Summary 5: Encapsulation, inheritance, polymorphism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.