多态即一个接口,多种实现

按照平常直接调用

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" class Person(object):
def __init__(self,name):
self.name = name class Student(Person):
def talk(self):
print('%s is studying'%self.name) class Teacher(Person):
def talk(self):
print('%s is teaching'%self.name) if __name__ == '__main__':
Student('John').talk()
Teacher('Jane').talk()

通过定义函数来实现多态

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" class Person(object):
def __init__(self,name):
self.name = name class Student(Person):
def talk(self):
print('%s is studying'%self.name) class Teacher(Person):
def talk(self):
print('%s is teaching'%self.name) def Person_Talk(n):
n.talk() if __name__ == '__main__':
s = Student('John')
t = Teacher('Jane')
Person_Talk(s)
Person_Talk(t)

通过父类调用来实现多态

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" class Person(object):
def __init__(self,name):
self.name = name @staticmethod
def Person_Talk(n):
n.talk() class Student(Person):
def talk(self):
print('%s is studying'%self.name) class Teacher(Person):
def talk(self):
print('%s is teaching'%self.name) if __name__ == '__main__':
s = Student('John')
t = Teacher('Jane')
Person.Person_Talk(s)
Person.Person_Talk(t)

最新文章

  1. jquery dataTable汉化(插件形式)
  2. [LeetCode] Pow(x, n) 求x的n次方
  3. calc 的使用
  4. 007. 自定义ListBox的item的宽高, 字体居中
  5. 非常非常好!写了好久 k-th-smallest-in-lexicographical-order
  6. tcprstat源码分析之tcp数据包分析
  7. iOS开发中使用静态库 .a 文件
  8. [HTTP] HTTP Verb
  9. Asp.net MVC中的ViewData与ViewBag(转)
  10. 【BZOJ1009】GT考试(KMP算法,矩阵快速幂,动态规划)
  11. SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)
  12. git > 2.3 实现同步盘的功能
  13. 软件测试自动化的最新趋势对开源测试管理软件ITEST的启示
  14. GIT-windows系统部署gitblit服务器
  15. [P3625][APIO2009]采油区域 (前缀和)
  16. deepin使用笔记-解决蓝牙设备开机自动开启的问题
  17. 安装Numpy方法
  18. python添加新的模块
  19. CentOS7切换到root用户和退回普通用户
  20. background的使用

热门文章

  1. JAVA8新特性——接口定义增强
  2. 在物理机安装CentOS6.5
  3. cplusplus.com
  4. 查询优化 | MySQL慢查询优化
  5. 2017-02-23 .NET Core Tools转向使用MSBuild项目格式
  6. Windows环境下CGAL的安装
  7. 按键(ESC ,F1,F2等)——wpf的命令处理方法
  8. srvany把程序作为Windows服务运行
  9. Javascript-理解事件总结
  10. Windows 运行中的命令