Python Object-oriented programming (II.)

Source: Internet
Author: User

This article transferred from the blog Garden Haizi's article http://www.cnblogs.com/dolphin0520/archive/2013/03/29/2986924.html

Again issued thanks to the sharing of Haizi, I read many times, benefited!

The basic definition and use of classes are described in the previous article, which only embodies one of the three main features of object-oriented programming: encapsulation.

Here's a look at two other features: inheritance and polymorphism.

In Python, if you want, you can have a class inherit a class, the inherited class is called a parent class or a superclass , or it can be called a base class , and the inherited class is called a subclass. And Python supports multiple inheritance, allowing a subclass to have more than one parent class.

I. Inheritance and multiple inheritance

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

#父类class superclassname:    block# Subclass Class Subclassname (Superclassname):    block

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. For example, students and teachers in universities can define a parent class Universitymember, and then class student and class teacher inherit the class Universitymember, respectively:

Class Universitymember:    def __init__ (self,name,age):        self.name = name        Self.age = Age    def getName (self ):        return self.name    def getage (self):        return Self.ageclass Student (universitymember):    def __init__ ( Self,name,age,sno,mark):        universitymember.__init__ (self,name,age)     #注意要显示调用父类构造方法, and pass parameter self        Self.sno = Sno        Self.mark = Mark    def getsno (self):        return Self.sno    def getmark (self):        Return Self.markclass Teacher (universitymember):    def __init__ (self,name,age,tno,salary):        universitymember.__init__ (self,name,age)        Self.tno = TNO        self.salary = Salary    def gettno (self):        return Self.tno    def getsalary (self):        return self.salary

Each member of the university has a name and age, and the student has 2 attributes of the number and the score, and the teacher has 2 attributes of the faculty number and salary, which can be seen 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.

For multiple inheritance, such as

Class Subclass (SUPERCLASS1,SUPERCLASS2)

One problem is that if subclass does not redefine the constructor method, which parent class is automatically called? Here's a point to remember: centered on the first parent class. If subclass redefined the construction method, it is necessary to display the constructor method of calling the parent class, at which point it is up to you to call which parent class is to be constructed, and if subclass does not redefine the constructor method, only the first parent class is executed. And if there is a method with the same name in SuperClass1 and SuperClass2, the method in the first parent class is called when the method is invoked by instantiating an object from a subclass.

Two. polymorphic

Polymorphism is a polymorphic form that determines its state at runtime and cannot determine its type at compile time. Polymorphism in Python is a bit different from Java and C + +, where variables in Python are weakly typed, not specified by their type when defined, and are determined at run time, as needed, by the type of the variable ( which the individual feels is also a manifestation of polymorphism). And Python itself is an explanatory language, not precompiled, so it only determines its state at run time, so some people say Python is a polymorphic language. Many places in Python can be characterized by polymorphism, such as the built-in function Len (object), theLen function can not only calculate the length of the string, but also to calculate the list, tuples and other objects in the number of data , here at run time through the parameter type to determine its specific calculation process , it is a manifestation of polymorphism. Some friends suggest that Python does not support polymorphism, which I totally disagree with.

Python Object-oriented programming (II.)

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.