Python : Polymorphism
2024-09-05 22:43:52
class Animal:
def __init__(self, name): # Constructor of the class
self.name = name
def talk(self): # Abstract method, defined by convention only
raise NotImplementedError("Subclass must implement abstract method") class Cat(Animal):
def talk(self):
return 'Meow!' class Dog(Animal):
def talk(self):
return 'Woof! Woof!' animals = [Cat('Missy'),
Cat('Mr. Mistoffelees'),
Dog('Lassie')] for animal in animals:
print animal.name + ': ' + animal.talk() # prints the following:
#
# Missy: Meow!
# Mr. Mistoffelees: Meow!
# Lassie: Woof! Woof!
最新文章
- BZOJ的两道osu概率DP easy与osu
- Struts2 有关于无法正常的使用通配符
- Java集合系列:-----------05LinkedList的底层实现
- java-数字类
- 用JAVA代码实现验证邮箱地址是否符合
- 第四课 Gallery的使用
- CSS font-family的順序
- PHP - 数学运算
- Webx3学习笔记(2)——基本流程
- 在vue 中使用Stylus
- 用系统为centos6的主机,搭建PXE服务器,实现批量安装centos6,7系统
- 13. Redis监控运维云平台CacheCloud
- Log4j配置记录
- Python大神成长之路: 第二次学习记录
- linux计划任务之crontab
- #ZLYD团队第二周项目总结
- day 90 DjangoRestFramework学习二之序列化组件
- Jquery chosen动态设置值 select Ajax动态载入数据 设置chosen和获取他们选中的值
- Generator 函数的异步应用
- 基于Python-Flask实现的网站例子