多态
class Horse:
def init(self, name) -> None:
self.name = name
def fature(self):
return '父亲-----马的名字: {0}'.format(self.name)
def mover(self):
print('马儿跑起来很潇洒')
class Monkey:
def init(self, name) -> None:
self.name = name
def fature(self):
return '父类-----驴的名字{0}'.format(self.name)
def speak(self):
print('驴跑的很欢快')
class Mule(Horse, Monkey):
def init(self, name, age) -> None:
super().init(name)
self.age = age
def mover(self):
return '子类----骡子的名字: {0}'.format(self.name, self.age)
mule1 = Mule(‘朵儿’, 2)
mule1.mover() # 继承父类horse的move()方法
mule1.speak() # 继承父类donkey的speak()方法
print(mule1.fature()) # 调用子类mule自己的fature方法
定义3个类,且类都含有同名的方法
class Animal:
def move(self):
print(‘动物的移动方式千奇百怪’)
class Dog(Animal):
def move(self):
print(‘小狗欢快的跑’)
class Rabbit(Animal):
def move(self):
print(‘小兔子是蹦蹦跳跳走路的’)
dog1 = Dog()
dog1.move()
rabbit1 = Rabbit()
rabbit1.move()
print(’--------------分割线’)
def fun(x):
x.move()
class Animals:
def move(self):
print(‘动物移动的方法千奇百怪’)
class Dogs(Animals):
def move(self):
print(‘小狗是欢快的跑’)
class Rabbits(Animals):
def move(self):
print(‘小兔子蹦蹦跳跳的走路’)
class Car:
def move(self):
print(‘小汽车飞驰在公路上’)
dog2 = Dogs()
rabbit2 = Rabbits()
car1 = Car()
Animal1 = Animals()
fun函数可以接收所有拥有move()方法的对象
fun(dog2)
fun(rabbit2)
fun(car1)
总代码如下:
谢谢观看,制作不易,不喜勿喷
如果喜欢,请点赞加关注哟
小白们,可以照着敲一遍哈