Python基础之继承
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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基础之继承的主要内容,如果未能解决你的问题,请参考以下文章