首页 > 代码库 > Python基础之继承

Python基础之继承

#继承实现


#父亲会书法,大儿子和小儿子会书法
#父亲一般能吃,大儿子吃超过,小儿子吃得少
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基础之继承