尝试尽可能简单地描述多态 [英] Try to describe polymorphism as easy as you can
本文介绍了尝试尽可能简单地描述多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何用一种易于理解的方式描述多态?
How can polymorphism be described in an easy-to-understand way?
我们可以在Internet和书籍上找到很多有关该主题的信息,例如 类型多态性 .但是,让我们尝试使其尽可能简单.
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.
推荐答案
这来自我的从一个类似的问题中回答.这是伪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屋!
查看全文