Python 多态调用简单demo

python多态

1、只有方法有多态,属性没有多态

2、多态的实现前提条件:继承和重写

3、子类属于父类 。。。可用isinstanace来判断该子类是否属于此父类对象

 1 class Man:
 2     def eat(self):
 3         print('吃饭了')
 4 class Englist(Man):
 5     def eat(self):
 6         print('英国人用叉子吃饭')
 7 class Chinese(Man):
 8     def eat(self):
 9         print('中国人用筷子吃饭')
10 class Indian(Man):
11     def eat(self):
12         print('印度人用右手吃饭')
13 
14 def manEat(m):
15     if isinstance(m,Man):
16         m.eat()
17     else:
18         print('不能吃饭')
19 
20 manEat(Chinese())
21 manEat(Englist())

 

posted @ 2020-11-18 10:21  hisweetyGirl  阅读(157)  评论(0编辑  收藏  举报