The inheritance of the Python Foundation

Source: Internet
Author: User

#继承实现


#父亲会书法, the older son and younger son can calligraphy.
#父亲一般能吃, the older son eats more than the youngest boy eats less
Class Father:
def write (self):
print ' I can write '
Class Oldbrother (father): #class A (B) subclass a inherits the parent class B
Def eat (self):
Print "I eat too Much"
Class Elderbrother (father):
def noeat (self):
Print "I can ' t eat"
Daerzi=oldbrother ()
Daerzi.write ()
Daerzi.eat ()

#多继承
Class Muniu:
Def eat (self):
print ' I can eat '
Class Gongniu:
def run (self):
print ' I can run '
Class Daniu (Muniu,gongniu): #class A (B,C,D)
Pass
Class Xiaoniu (Muniu):
Pass
Daniu=daniu ()
Daniu.eat ()
Daniu.run ()
Xiaoniu=xiaoniu ()
Xiaoniu.eat ()
#xiaoniu. Run ()

#多继承冲突, inherited from left to right (same time as parent method name)
Class Muniu:
Def eat (self):
print ' I can eat '
Class Gongniu:
def run (self):
print ' I can run '
Def eat (self):
print ' I can eat,too '

Class Daniu (Muniu,gongniu): #class A (B,C,D)
Pass
Daniu=daniu ()
Daniu.eat ()

The inheritance of the Python Foundation

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.