了解多态

  多态指的是一类事物有多种形态

.定义:多态是一中使用对象的方式,更容易编写出通用的代码,做出通用的编程,一适应需求的不断变化

实现步骤:

  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-类的多态的理解的更多相关文章

  1. python中对多态的理解

    目录 python中对多态的理解 一.多态 二.多态性 三.鸭子类型 python中对多态的理解 一.多态 多态是指一类事物有多种形态,比如动物类,可以有猫,狗,猪等等.(一个抽象类有多个子类,因而多 ...

  2. Python 类的多态的运用

    #类的多态的运用 #汽车类 class Car(object): def move(self): print("move ...") #汽车商店类 class CarStore(o ...

  3. Python类的多态的例子

    1 # -*- coding: utf-8 -*- 2 # 类的多态 3 4 # 定义Person父类 5 class Person(object): 6 def __init__(self, nam ...

  4. Python类总结-多态及鸭子类型

    Python天生支持多态. 什么是多态: 一类事务的多种形态. 多态的一个例子 class Alipay(): def pay(self,money): print('用支付宝支付了%s元' % mo ...

  5. python类的多态

    1. 什么是多态     多态指的是同一种/类事物的不同形态   2. 为何要用多态     多态性:在多态的背景下,可以在不用考虑对象具体类型的前提下而直接使用对象     多态性的精髓:统一   ...

  6. python类的多态、多态性

    多态:多态指的是一类事物有多种形态 多态性: class Animal: def run(self): raise AtrributeError("子类必须实现这种方法") cla ...

  7. Python类与对象的理解

    注意python的类对象与实例对象的区分 类对象与实例对象是相对的,例如:a=1,那么a就是int的一个实例对象,这里的a相对于int来说,a是实例对象,int是类对象.但是int同时又是type的实 ...

  8. Python 类的多态

    #python的多态 class Dog(): def eat(self): print("i am dog , eat something . ") class Cat(): d ...

  9. Python类(四)-多态

    多态即一个接口,多种实现 按照平常直接调用 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" class Person(obje ...

  10. python中对多态和多态性的理解

    python中对多态的理解 一.多态 多态是指一类事物有多种形态,比如动物类,可以有猫,狗,猪等等.(一个抽象类有多个子类,因而多态的概念依赖于继承) import abc class Animal( ...

随机推荐

  1. 如何强制删除 baidu/tempdata/con.dat 的垃圾文件! How to fix locked SD card: 读卡器 损坏,补救措施!

    https://www.youtube.com/watch?v=y2c37dcxNto&feature=youtu.be 使用windows command prompt 强制删除 baidu ...

  2. CSS Grid Layout In Action

    CSS Grid Layout In Action CSS 异形网格布局实战 refs https://static-nginx-online.fbcontent.cn/tutor/tutor-ybc ...

  3. npm & config settings

    npm & config settings how to check npm config settings https://docs.npmjs.com/cli/config $ npm c ...

  4. Azure 信用卡扣款 1 美元 & Azure 中国客服

    Azure 信用卡扣款 1 美元 & azure 中国客服 Azure 免费帐户常见问题 https://azure.microsoft.com/zh-cn/free/free-account ...

  5. js & input event & input change event

    js & input event & input change event vue & search & input change <input @click=& ...

  6. robots.txt

    robots.txt A robots.txt file tells search engine crawlers which pages or files the crawler can or ca ...

  7. nasm astrstr函数 x86

    xxx.asm: %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export as ...

  8. C++使用libcurl进行http通讯

    借着curl 7.75.0版本更新, 最近又下载下来玩了玩, 在此做个简单记录 1.环境搭建 首先是libcurl动态库, 自己下载源码编译的话如果要使用https协议还要下载OpenSSL和libs ...

  9. el-input输入框的readonly属性

    readonly属性是Boolean类型,默认值为false.readonly值为true表示只读. <el-col :span="12"> <el-form-i ...

  10. 文件下载:报错The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'

    前言:这篇文件下载的后台代码太繁琐,建议参考https://www.cnblogs.com/zwh0910/p/13745947.html 前端: <el-button type="p ...