码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础之继承

时间:2017-06-25 16:11:57      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:class   sel   冲突   other   python   can   elf   ldb   dbr   

#继承实现


#父亲会书法,大儿子和小儿子会书法
#父亲一般能吃,大儿子吃超过,小儿子吃得少
class father:
    def write(self):
        print   ‘i can write‘
class oldbrother(father):#class A(B)子类A继承父类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()
          
#多继承冲突,从左到右继承(父类方法名相同时候)
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()

Python基础之继承

标签:class   sel   冲突   other   python   can   elf   ldb   dbr   

原文地址:http://www.cnblogs.com/jietingting/p/7076963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!