zoukankan      html  css  js  c++  java
  • Python之面向对象多态

    Python之面向对象多态

       多态与多态性:

        多态:

          多态是指一类事物有多种形态,一个抽象类有多个子类,因而多态的概念依赖于继承。

          1、序列类型有多种形态:字符串、列表、元组。

          2、动物有多种形态:People、Dog、Pig。

    #多态:同一种事物的多种形态,动物分为人类,猪类(在定义角度)
    class Animal:
        def run(self):
            raise AttributeError('子类必须实现这个方法')
    
    
    class People(Animal):
        def run(self):
            print('人正在走')
    
    class Pig(Animal):
        def run(self):
            print('pig is walking')
    
    
    class Dog(Animal):
        def run(self):
            print('dog is running')
    
    peo1=People()
    pig1=Pig()
    d1=Dog()
    
    peo1.run()
    pig1.run()
    d1.run()

      多态性:

        一种调用方式,不同的执行结果。

        多态性依赖于继承、多态性定义统一的接口。 

     

    #多态性:一种调用方式,不同的执行效果(多态性)
    def func(obj):
        obj.run()
    
    func(peo1)
    func(pig1)
    func(d1)
    
    
    # peo1.run()
    # pig1.run()
    
    
    # 多态性依赖于:
    #     1.继承
    #     2.定义接口
    ##多态性:定义统一的接口,
    def func(obj): #obj这个参数没有类型限制,可以传入不同类型的值
        obj.run() #调用的逻辑都一样,执行的结果却不一样。由继承决定。
    
    
    func(peo1)
    func(pig1)
    
    func(d1)
  • 相关阅读:
    一条命令深度清理你的mac
    将以太坊封装为 ERC20
    golang subprocess tests
    go 笔记
    readme
    如何使用 channel
    修改vscode caipeiyu.writeCnblog ,简化博客发布
    thedao
    firefox 59 无法使用 pac 代理上网
    scrapy简单使用
  • 原文地址:https://www.cnblogs.com/george92/p/9224282.html
Copyright © 2011-2022 走看看