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

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

问题描述

有人可以给我一个现实的多态性实例吗?我的教授告诉了我一个我一直听到的关于+运算符的古老故事. a+b = c2+2 = 4,所以这是多态性.我确实不能将这样的定义与自己联系起来,因为我已经阅读并重新阅读了许多书籍.

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

推荐答案

查看Wikipedia示例:在较高级别上非常有帮助:

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".

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

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.

以您的示例为例:

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天全站免登陆