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( ...
随机推荐
- SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解
题意: 求\(n\)个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,然后枚举所有串.对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少), ...
- Creative Commons : CC (知识共享署名 授权许可)
1 https://creativecommons.org/ Keep the internet creative, free and open. Creative Commons help ...
- Android 如何设置 WebView 的屏幕占比
Android 如何设置 WebView 的屏幕占比 由于 Android 适用于具有各种屏幕尺寸和像素密度的设备,因此您在设计网页时应将这些因素纳入考虑范围,以便您的网页始终以合适的尺寸显示. We ...
- 如何用 js 实现一个类似微信红包的随机算法
如何用 js 实现一个类似微信红包的随机算法 js, 微信红包, 随机算法 "use strict"; /** * * @author xgqfrms * @license MIT ...
- Web 安全 & cookies & HttpOnly
Web 安全 & cookies & HttpOnly cookie HttpOnly 禁止 js 读取 cookie 的方法 HttpOnly 实现原理 document.cooki ...
- funny 生成器
funny 生成器 https://www.zhihu.com/question/380741546/answer/1190570384 举牌小人生成器 https://small-upup.upup ...
- css animation & animationend event & onanimationend
css animation & animationend event & onanimationend https://developer.mozilla.org/en-US/docs ...
- 微信公众号 bug
微信公众号 bug web bug refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
- how to convert SVG shapes to polygon
how to convert SVG shapes to polygon 如何将 svg 的 rect 转换成 polygon rect.circle.ellipse.line.polyline.po ...
- macOS & Nginx
macOS & Nginx ngnix # 使用 brew 安装(如果没有 brew 命令,需要自行安装 brew) $ brew install nginx $ nginx -h # 查看 ...