Python Object-oriented Programming (iii) inheritance and polymorphism

Source: Internet
Author: User
Tags inheritance in python

In the previous article, we talked about the basic definition and usage of classes, which only embodies one of the three characteristics of object-oriented programming: encapsulation. Let's look at two other features: inheritance and polymorphism.

In Python, you can have a class inherit a class, which is called a parent class or 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 multiple parent classes.


I. Inheritance and multiple inheritance

Class Superclass ():
          ...

Class Subclass (Superclass):
          .....
         

When defining a class, you can immediately follow the class name with a pair of parentheses, specifying the inherited parent class in parentheses, and separating multiple parent classes with commas if there are multiple parent classes.


Class Universitymember:

    def __init__ (self,name,age):
        self.name = name
        Self.age = Age

    def getName (self ): Return
        self.name

    def getage (self): return
        Self.age

class Student (Universitymember):

    def __ Init__ (Self,name,age,sno,mark):
        universitymember.__init__ (self,name,age)     #注意要显示调用父类构造方法, passing arguments self
        Self.sno = Sno
        Self.mark = Mark

    def getsno (self): return
        self.sno

    def getmark (self):
        Return Self.mark



class 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

1 in Python, if the parent class and subclass have redefined the constructor __init (), the constructor method of the subclass does not automatically call the parent class's constructor when the subclass is instantiated, and the call must be displayed in the subclass.

2 If you need to call a method of the parent class in a subclass, you need to use the parent class name. Method called in this way, notice to pass the self parameter past.

For inheritance relationships, subclasses inherit all public properties and methods of the parent class, which can be invoked through the parent class name in the subclass, and for private properties and methods, subclasses are not inherited, and therefore cannot be accessed through the parent class name in subclasses.

For multiple inheritance, such as

Class Subclass (SUPERCLASS1,SUPERCLASS2)

One problem at this point is that if subclass does not redefine the constructor method, it automatically invokes the constructor of which parent class. Here's one point to remember: The first parent class is the center. If subclass redefine the construction method, you need to display the constructor method that invokes the parent class, at which point the constructor of the parent class is determined by yourself, and if subclass does not redefine the constructor method, only the first parent class is constructed. And if a method of the same name is used in SuperClass1 and SuperClass2, the method is called in the first parent class through the instantiated object of the subclass.


Two. Polymorphism

The

polymorphism, which is a variety of forms, determines its state at runtime and cannot be determined at compile time, which is polymorphism. Polymorphism in Python is somewhat different from Java and polymorphism in C + +. A variable in Python is a weak type, defined without specifying its type, and it determines the type of the variable at run time as needed (which the individual feels is a manifestation of polymorphism), and Python itself is an interpretive language, It is not precompiled, so it only determines its state at run time, and it is also said that Python is a polymorphic language. In python many places can embody polymorphism, such as the built-in function Len (object), the Len function can not only calculate the length of strings, but also 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, and I totally disagree with it. Python embodies the root cause of polymorphism in its unique way I think there are two points: 1 Python is an explanatory language; 2 The variables in Python are weakly typed. So Python's polymorphic approach is decidedly different from Java's, but it is too one-sided to think that Python does not support polymorphism because it is different from the way it is manifested in Java.

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.