尝试尽可能简单地描述多态性 [英] Try to describe polymorphism as easy as you can

查看:25
本文介绍了尝试尽可能简单地描述多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以易于理解的方式描述多态性?

How can polymorphism be described in an easy-to-understand way?

我们可以在互联网和书籍上找到很多关于该主题的信息,例如Type多态性.但让我们尽量让它变得简单.

We can find a lot of information about the subject on the Internet and books, like in Type polymorphism. But let's try to make it as simple as we can.

推荐答案

这是来自我的 answer 来自一个类似的问题.这是伪 C#/Java 中的多态性示例:

This is from my answer from a similiar question. Here's an example of polymorphism in pseudo-C#/Java:

class Animal
{
    abstract string MakeNoise ();
}

class Cat : Animal {
    string MakeNoise () {
        return "Meow";
    }
}

class Dog : Animal {
    string MakeNoise () {
        return "Bark";
    }
}

Main () {
   Animal animal = Zoo.GetAnimal ();
   Console.WriteLine (animal.MakeNoise ());
}

Main() 方法不知道动物的类型,它取决于 MakeNoise() 方法的特定实现的行为.

The Main() method doesn't know the type of the animal and depends on a particular implementation's behavior of the MakeNoise() method.

这篇关于尝试尽可能简单地描述多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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