Closed. This question needs to be more focused。它当前不接受答案。












想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。

6年前关闭。



Improve this question




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

我们可以在Internet和书籍上找到很多有关该主题的信息,例如Type polymorphism。但是,让我们尝试使其尽可能简单。

最佳答案

这是我的answer来自一个类似的问题。这是伪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()方法的特定实现的行为。

09-09 20:00
查看更多