多态的实际例子 [英] Practical example of Polymorphism

查看:28
本文介绍了多态的实际例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个真实的、实际的多态性例子?我的教授跟我讲了我一直听到的关于 + 运算符的老故事.a+b = c2+2 = 4,所以这就是多态.我真的无法将自己与这样的定义联系起来,因为我已经在很多书中阅读并重新阅读了这一点.

我需要的是一个真实世界的代码示例,我可以真正将其联系起来.

例如,这里有一个小例子,以防万一你想扩展它.

<预><代码>>>>类人(对象):def __init__(self, name):self.name = 姓名>>>班级学生(人):def __init__(自我,姓名,年龄):超级(学生,自我).__init__(姓名)self.age = 年龄

解决方案

查看维基百科示例:它在高层次上很有帮助:

类动物:def __init__(self, name): # 类的构造函数self.name = 姓名def talk(self): # 抽象方法,仅按约定定义raise NotImplementedError("子类必须实现抽象方法")类猫(动物):def 谈话(自我):返回喵!"类狗(动物):def 谈话(自我):返回'哇!纬!'动物 = [猫('小姐'),Cat('Mistoffelees先生'),狗('Lassie')]对于动物中的动物:打印动物名称 + ': ' + 动物谈话()# 打印以下内容:##小姐:喵!# Mistoffelees 先生:喵!# 莱西:哇!纬!

请注意以下几点:所有动物都会说话",但它们的说话方式不同.因此,谈话"行为是多态的,因为它根据动物的不同而不同.所以,抽象的动物"概念实际上并不是说话",而是特定的动物(如狗和猫)对说话"动作有具体的实现.

类似地,许多数学实体中都定义了加"运算,但在特定情况下,您可以根据特定规则加":1+1 = 2,但是 (1+2i)+(2-9i)=(3-7i).

多态行为允许您在抽象"级别指定通用方法,并在特定实例中实现它们.

例如:

class Person(object):def pay_bill(自己):引发 NotImplementedError百万级(人):def pay_bill(自己):打印给你!保留零钱!"班级研究生(人):def pay_bill(自己):打印我可以欠你十块钱或者洗碗吗?"

你看,百万富翁和研究生都是人.但说到付账,他们具体的付账"动作就不一样了.

Can anyone please give me a real life, practical example of Polymorphism? My professor tells me the same old story I have heard always about the + operator. a+b = c and 2+2 = 4, so this is polymorphism. I really can't associate myself with such a definition, since I have read and re-read this in many books.

What I need is a real world example with code, something that I can truly associate with.

For example, here is a small example, just in case you want to extend it.

>>> class Person(object):
    def __init__(self, name):
        self.name = name

>>> class Student(Person):
    def __init__(self, name, age):
        super(Student, self).__init__(name)
        self.age = age

解决方案

Check the Wikipedia example: it is very helpful at a high level:

class Animal:
    def __init__(self, name):    # Constructor of the class
        self.name = name
    def talk(self):              # Abstract method, defined by convention only
        raise NotImplementedError("Subclass must implement abstract method")

class Cat(Animal):
    def talk(self):
        return 'Meow!'

class Dog(Animal):
    def talk(self):
        return 'Woof! Woof!'

animals = [Cat('Missy'),
           Cat('Mr. Mistoffelees'),
           Dog('Lassie')]

for animal in animals:
    print animal.name + ': ' + animal.talk()

# prints the following:
#
# Missy: Meow!
# Mr. Mistoffelees: Meow!
# Lassie: Woof! Woof!

Notice the following: all animals "talk", but they talk differently. The "talk" behaviour is thus polymorphic in the sense that it is realized differently depending on the animal. So, the abstract "animal" concept does not actually "talk", but specific animals (like dogs and cats) have a concrete implementation of the action "talk".

Similarly, the "add" operation is defined in many mathematical entities, but in particular cases you "add" according to specific rules: 1+1 = 2, but (1+2i)+(2-9i)=(3-7i).

Polymorphic behaviour allows you to specify common methods in an "abstract" level, and implement them in particular instances.

For your example:

class Person(object):
    def pay_bill(self):
        raise NotImplementedError

class Millionare(Person):
    def pay_bill(self):
        print "Here you go! Keep the change!"

class GradStudent(Person):
    def pay_bill(self):
        print "Can I owe you ten bucks or do the dishes?"

You see, millionares and grad students are both persons. But when it comes to paying a bill, their specific "pay the bill" action is different.

这篇关于多态的实际例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆