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

多态性:

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

  1. python类的多态

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

  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 类的多态

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

  6. Python类(四)-多态

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

  7. Python之路【第十篇】:Python面向对象之多态、多态性

    阅读目录 一 多态 多态指的是一类事物有多种形态 动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.ABCMeta): #同一类事物:动物 @ab ...

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

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

  9. day21-5 类的多态与多态性

    类的多态与多态性 多态 多态指的是一类事物有多种形态,如动物有多种形态:人.狗.猪 import abc class Animal(metaclass=abc.ABCMeta): # 同一类事物:动物 ...

随机推荐

  1. Upgrading CentOS 6 to CentOS 7

    Upgrading CentOS 6 to CentOS 7 November 15th, 2018 — whplus PRE TASKS There are some tasks you can d ...

  2. Httpwatch教程

    启动Httpwatch 从IE的“查看”—“浏览器栏”—“HttpWatch”启动HttpWatch.如下图所示: 以下是HttpWatch程序界面 以下用登录我的邮箱mail.163.com例子来展 ...

  3. C# U盘扫描

    无意中看到的一个例程,保留一份.   以供后用.     using System; using System.Collections.Generic; using System.ComponentM ...

  4. 装饰器中functools的用处

    定义一个最简单的装饰器 def user_login_data(f): def wrapper(*args, **kwargs): return f(*args, **kwargs) return w ...

  5. analysis_tools

  6. Grace模式、Saint模式

    一.probe(后端探针) 探测后端,确定他们是否健康,返回的状态用req.backend.healthy核对 backend b1 { .host = "127.0.0.1"; ...

  7. MQTT协议探究(一)

    1 准备阶段 MQTT客户端:https://www.cnblogs.com/linzhanfly/p/9923577.html WireShark MQTT服务器(iot.eclipse.org) ...

  8. *4.1 所有类型都从System.Object派生

  9. 操作系统中堆(heap)与栈(stack)的区别

    主要区别如下: 一.空间分配: 1.堆(操作系统):一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收,分配方式类似于链表.PS:java中都是系统GC,程序员无法进行GC. 2.栈(操作 ...

  10. canvas之五角星的绘制

    <html> <head> <meta charset=utf-8> <title>绘制简单图形线及矩形</title> <style ...