python-类的多态的理解
了解多态
多态指的是一类事物有多种形态
.定义:多态是一中使用对象的方式,更容易编写出通用的代码,做出通用的编程,一适应需求的不断变化
实现步骤:
1.定义父类,并提供公共方法
2.定义子类,并重写父类方法
3.传递子类对象给调用者,可以看到子类执行的效果不同
#coding:utf-8
2 class Dog(object):
3 def work(self):
4 print("指")
5
6 class Onedog(Dog):
7 #重写父类方法
8 def work(self):
9 print("中国")
10
11 class TwoDog(Dog):
12 #重写父类方法
13 def work(self):
14 print("英国")
15
16 class Person(object):
17 def work_with_dog(self,dog):
18 #传入不同的对象,执行不同的代码,即不同的work函数
19 dog.work()
20
21 a = Onedog()
22 b = TwoDog()
23 c = Person()
24
25 c.work_with_dog(a)
26 c.work_with_dog(b)
27 #运行结果
中国
英国 ~
python-类的多态的理解的更多相关文章
- python中对多态的理解
目录 python中对多态的理解 一.多态 二.多态性 三.鸭子类型 python中对多态的理解 一.多态 多态是指一类事物有多种形态,比如动物类,可以有猫,狗,猪等等.(一个抽象类有多个子类,因而多 ...
- Python 类的多态的运用
#类的多态的运用 #汽车类 class Car(object): def move(self): print("move ...") #汽车商店类 class CarStore(o ...
- Python类的多态的例子
1 # -*- coding: utf-8 -*- 2 # 类的多态 3 4 # 定义Person父类 5 class Person(object): 6 def __init__(self, nam ...
- Python类总结-多态及鸭子类型
Python天生支持多态. 什么是多态: 一类事务的多种形态. 多态的一个例子 class Alipay(): def pay(self,money): print('用支付宝支付了%s元' % mo ...
- python类的多态
1. 什么是多态 多态指的是同一种/类事物的不同形态 2. 为何要用多态 多态性:在多态的背景下,可以在不用考虑对象具体类型的前提下而直接使用对象 多态性的精髓:统一 ...
- python类的多态、多态性
多态:多态指的是一类事物有多种形态 多态性: class Animal: def run(self): raise AtrributeError("子类必须实现这种方法") cla ...
- Python类与对象的理解
注意python的类对象与实例对象的区分 类对象与实例对象是相对的,例如:a=1,那么a就是int的一个实例对象,这里的a相对于int来说,a是实例对象,int是类对象.但是int同时又是type的实 ...
- Python 类的多态
#python的多态 class Dog(): def eat(self): print("i am dog , eat something . ") class Cat(): d ...
- Python类(四)-多态
多态即一个接口,多种实现 按照平常直接调用 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" class Person(obje ...
- python中对多态和多态性的理解
python中对多态的理解 一.多态 多态是指一类事物有多种形态,比如动物类,可以有猫,狗,猪等等.(一个抽象类有多个子类,因而多态的概念依赖于继承) import abc class Animal( ...
随机推荐
- mysql(五)--性能优化总结
1 优化思路 作为架构师或者开发人员,说到数据库性能优化,你的思路是什么样的? 或者具体一点,如果在面试的时候遇到这个问题:你会从哪些维度来优化数据库, 你会怎么回答? 我们在第一节课开始的时候讲了, ...
- vue component :is
vue component :is Vue <component> element https://vuejs.org/v2/guide/components.html#Dynamic-C ...
- npm ci All In One
npm ci All In One npm 性能优化 npm ci 使用干净的面板安装项目 https://docs.npmjs.com/cli/v6/commands/npm-ci # npm cl ...
- Programming Interview Questions Websites All In One
Programming Interview Questions Websites All In One 编程面试刷题网站 http://highscalability.com/ https://tri ...
- Free Video Player All In One
Free Video Player All In One VLC media player https://github.com/videolan/vlc VideoLAN https://www.v ...
- How to enable HTTPS for local development in macOS using Chrome
How to enable HTTPS for local development in macOS using Chrome HTTPS, macOS, Chrome local HTTPS htt ...
- Vue 组件之间通信 All in One
Vue 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 https://stackblitz.com/edit/vue-parent-child-commutation?fil ...
- 二叉搜索树 & 二叉树 & 遍历方法
二叉搜索树 & 二叉树 & 遍历方法 二叉搜索树 BST / binary search tree https://en.wikipedia.org/wiki/Binary_searc ...
- hackr.io & Programming Courses & Programming Tutorials
hackr.io & Programming Courses & Programming Tutorials the Best Programming Courses & Tu ...
- D language
D language https://en.wikipedia.org/wiki/D_(programming_language) Dart https://dlang.org/ flutter fr ...