python6.3类的继承与多态

class Animal(object):

def __init__(self,color):
self.color=color

def eat(self):
print("动物在吃!")

def run(self):
print("动物在跑!")

class Cat(Animal):#继承Animal类
def eat(self):
print("猫在吃鱼!")

class Dog(Animal):
def __init__(self,name,age,color):
super(Dog,self).__init__(color)#调用父类的初始化方法
self.name=name
self.age=age
def eat(self):
print("狗在啃骨头!")
#类的继承
cat=Cat("黑色")
print(cat.color)
cat.eat()
cat.run()
dog=Dog("小白",7,"黑色")
dog.eat()
dog.run()


#类的多态
def feed(obj):
obj.eat()
an=Animal("黄")
cat=Cat("橘色")
dog=Dog("小黑",5,"黑色")
feed(cat)

猜你喜欢

转载自www.cnblogs.com/lma0702/p/11111041.html
今日推荐