__metaclass__=type #定义使用新式类
class Bird:
def __init__(self):
self.hungry = True #父类中的属性hungry
def eat(self):
if self.hungry:
print 'Ha...Ha'
self.hungry=False
else:
def __init__(self):
self.name='pets'
def eat(self):
print 'I can eat plant'
class songBird(Pets,Bird):
def __init__(self,name='k-bird'):
super(songBird,self).__init__() #使用super进行继承
self.name = name
def song(self):
self.eat() # 先吃饭,再唱歌,无可厚非......哈哈。高兴的别太早,注意此处是调用Pets类中的eat()方法
print 'Hello,everyone, I am %s,Do I sing well?' % self.name
sb=songBird('Penguis')
class Bird:
def __init__(self):
self.hungry = True #父类中的属性hungry
def eat(self):
if self.hungry:
print 'Ha...Ha'
self.hungry=False
else:
print 'No,Thanks'
def __init__(self):
self.name='pets'
def eat(self):
print 'I can eat plant'
class songBird(Pets,Bird):
def __init__(self,name='k-bird'):
super(songBird,self).__init__() #使用super进行继承
self.name = name
def song(self):
self.eat() # 先吃饭,再唱歌,无可厚非......哈哈。高兴的别太早,注意此处是调用Pets类中的eat()方法
print 'Hello,everyone, I am %s,Do I sing well?' % self.name
sb=songBird('Penguis')
sb.song()
继承多个父类中的同一个方法(如调用Bird类、Pets类中共同的eat方法),则根据类名顺序调用第一个类中的方法。如果是class songBird(Pets,Bird),则songBird类中调用eat方法,调用的是Pets类中的eat方法;如果class songBird(Bird,Pets),则songBird类中调用eat方法,调用的是Bird类中的eat方法。
本文探讨了鸟类作为宠物的特点,包括它们的饥饿状态如何影响行为,并通过代码展示了如何实现宠物类及其子类的功能,特别关注了继承和方法调用的顺序。详细介绍了鸟类宠物的进食和歌唱行为之间的关系。
827

被折叠的 条评论
为什么被折叠?



