Python Object-oriented inheritance

Source: Internet
Author: User

Nothing to say, inheritance is mainly inherited by some parents, the code is very specific

#!/usr/bin/env python #coding: Utf-8class Father (object): #新式类    def __init__ (self):        self.name= ' Liu ' self        . Familyname= ' Yan '            def Lee (self):        print ' I am the parent function Lee '        def Allen (self):        print "I am the parent function Allen"        class Son (Father):    def __init__ (self):        #Father. __init__ (self)   #经典类运行父类构造函数        Super (son,self). __init__ ()  #新式类运行父类构造函数        self.name= ' Feng '            def Aswill (self): #子类新增函数        print ' Son.bar '        def Lee: #重写父类函数Lee        print ' subclass overrides the parent class function Lee '    s1=son () print "inherits the parent's last name" + S1. Familynameprint "Overrides the name of the parent class", S1.names1.Lee () #子类重写了父类函数Lees1. Allen () #子类继承了父类函数Allen

The order of inheriting multiple classes, the classic class inheritance is depth first, is a bug, the new class is breadth first, should be to use the new class to define the class

New class

Class A (object): #新式类的写法    def __init__ (self):        print ' This is from A '        def Test (self):        print ' The IS test fr Om A ' class B (a):    def __init__ (self):        print "The IS from B"        class C (a):    def __init__ (self):        print " This was from C "        def Test (self):        print" The IS test from C "                class D (b,c):    def __init__ (self):        pri NT ' This is D '        t1=d () t1.test ()


Classic class

Class A:    def __init__ (self):        print ' The From a '        def Test (self):        print ' This is test from a ' class B (a):    def __init__ (self):        print "The IS from B"        class C (A):    def __init__ (self):        print "The" from C "
   def Test (self):        print "The IS test from C"                class D (b,c):    def __init__ (self):        print ' This is D ' 
   t1=d () t1.test ()




Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Python Object-oriented inheritance

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.