python类的多态、多态性
多态:多态指的是一类事物有多种形态
多态性:
class Animal:
def run(self):
raise AtrributeError("子类必须实现这种方法")
class Person(Animal):
pass
p = Person()
p.run()
通过父类主动抛出一个异常,告诉你子类中必须自己要写这个方法
改子类如下
class Person(Animal):
def run(self):
print("人跑")
再定义几个子类
def Dog(Animal):
def run(self):
print("狗跑")
def Pig(Animal):
def run(self):
print("猪跑")
person = Person()
dog = Dog()
pig = Pig()
person.run()
dog.run()
pig.run()
上面这就是多态,同属一个父类,但是他们在run这个方法上表现出不能的形态,这就是多态。
那什么是多态性呢?
还是举电脑组装的例子
电脑主机生产商定义了电脑这个类
class Computer():
def usb_run(self):
raise AtrributeError("USB设备自己要开发run方法")
def usb_insert(obj):
obj.run()
class KeyBoard(Computer):
def usb_run(self):
print("键盘插入了")
class Mouse(Computer):
def usb_run(self):
print("鼠标插入了")
k = KeyBoard()
m = Mouse()
usb_insert(k)
usb_insert(m)
简单理解就是定义了一个函数,函数里执行了多态的共同方法,
但是并不区分是哪个对象传送过来的,具体对象需要通过向函数传递参数实现,这种实现方法就是多态性。
python类的多态、多态性的更多相关文章
- python类的多态
1. 什么是多态 多态指的是同一种/类事物的不同形态 2. 为何要用多态 多态性:在多态的背景下,可以在不用考虑对象具体类型的前提下而直接使用对象 多态性的精髓:统一 ...
- 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 类的多态
#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(metaclass=abc.ABCMeta): #同一类事物:动物 @ab ...
- python中对多态和多态性的理解
python中对多态的理解 一.多态 多态是指一类事物有多种形态,比如动物类,可以有猫,狗,猪等等.(一个抽象类有多个子类,因而多态的概念依赖于继承) import abc class Animal( ...
- day21-5 类的多态与多态性
类的多态与多态性 多态 多态指的是一类事物有多种形态,如动物有多种形态:人.狗.猪 import abc class Animal(metaclass=abc.ABCMeta): # 同一类事物:动物 ...
随机推荐
- 1.3Security:权限管理,过滤、监听、拦截
Security:权限管理 常用权限拦截器 SecurityContextPersistenceFilter 以前是HttpSesstionContextIntegrationFilter,位于过滤器 ...
- (十四)springMvc 对 restful 的支持
文章目录 restful 的概念 配置支持 restful 的前端控制器 配置不拦截静态资源 restful 的概念 restful 是一种开发理念: 对 url 进行规范 每个 url 代表一个资源 ...
- 南昌网络赛J. Distance on the tree 树链剖分
Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...
- loj 10117 简单题
#include<iostream> #include<cstdio> #include<cctype> using namespace std; inline i ...
- python — 函数基础知识(二)
目录 1 返回值 2 作用域 3 函数小高级 4 函数中高级 1 返回值 def func(arg): # .... return 9 # 返回值为9 默认:return None val = fun ...
- spark异常篇-Removing executor 5 with no recent heartbeats: 120504 ms exceeds timeout 120000 ms 可能的解决方案
问题描述与分析 题目中的问题大致可以描述为: 由于某个 Executor 没有按时向 Driver 发送心跳,而被 Driver 判断该 Executor 已挂掉,此时 Driver 要把 该 Exe ...
- 【搜索】n的约数
题目链接:传送门 [题解]: 考察dfs和质因数分解,首先开一个prime数组. 参数解释: 1.当前值的大小.[利用题目的n来控制范围] 2.控制下界,每次都是以某一个质数开始搜索, pos 3.控 ...
- 牛客 P21336 和与或 (数位dp)
大意: 给定数组$R$, 求有多少个数组$A$, 满足$0\le A_i \le R_i$且$A_0+...+A_{N-1}=A_0\space or ...\space or \space A_{N ...
- UVA Tress in a Wood
https://vjudge.net/problem/UVA-10214 题意:给定一个坐标系.|x|<=a, |y|<=b 求坐标系中有多少点是可以从原点直接看到(即从原点和一个点连线, ...
- Jmeter4.0---- 修改jmeter源代码(18)
1.说明 jmeter本身功能很强大,但是在使用的时候我们会发现有些想法jmeter无法帮我们实现,这个时候就需要我们细节去修改一下它的源代码,来满足我们的需求. * 仅供参考 2.步骤 第一步: j ...