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)
原文地址:https://www.cnblogs.com/lma0702/p/11111041.html
时间: 2024-11-18 16:39:11