多态性 - 重载/覆盖 [英] Polymorphism - Overloading/Overriding

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

问题描述

我知道StackOverflow已经解决了这个问题,并且已经发布了很多问题。 我可能已经读过它们中的每一个但是,有这个令人唠叨的疑问:我认为我理解重载很好,并且覆盖。让我变形的是多态性。

I know that this question has been done to death at StackOverflow and that there are numerous questions posted on this already. I've probably read every one of them and yet, there's this niggling doubt: I think I understand Overloading pretty well, and Overriding. What gets me is Polymorphism.

例如,此问题的接受答案 shape.Draw()解释这个。我很困惑这与Overriding有什么不同(其他时候我对它与Overloading的不同之处感到困惑)。

For example, the accepted answer to this question explains this with shape.Draw(). I'm confused as to how this is different from Overriding (other times I'm confused with how it is different from Overloading).

此外 - 多态性本身就意味着派生从抽象类? (我想我几乎所有关于这个主题的答案都使用了一个抽象的动物类,让一只猫和一只狗喵喵/吠叫:)

Also - does Polymorphism inherently mean deriving from an abstract class? (I think almost all the answers I've read on the topic uses an abstract animal class and makes a cat and a dog meow/bark :)

To总结,我的问题是:


  1. 什么是多态性wrt重载和覆盖?

  1. What is Polymorphism w.r.t. Overloading and Overriding?

有人可以在没有抽象类的情况下解释多态性 - 谢谢!

Could somebody please explain Polymorphism without an abstract class - thanks!

重载/覆盖不是多态的子类型,是吗?

Overloading/Overriding are not subtypes of Polymorphism, are they?

编辑添加第3个问题并修改第二个问题。

Edited to add a 3rd question and modify the 2nd question.

推荐答案

回答你的问题:


  1. 根据用于调用它的对象,可以在运行时中选择更专业的方法。

  2. 当然。多态性可能在没有涉及抽象类的情况下发生。

  3. 不,重载/覆盖不是多态的类型。

  1. It's the ability to select more specialized methods in runtime depending on the object being used to call it.
  2. Of course. Polymorphism could occur with no abstract classes involved.
  3. No, overloading/overriding are not types of polymorphism.

这是一个多态性发生的例子,没有涉及抽象类。

Here's an example with polymorphism happening with no abstract classes involved.

// non abstract
class A
{
    public void a()
    {
        System.out.println("Hello from A");
    }
}

class B
   extends A
{
    @Override
    public void a()
    {
        System.out.println("Hello from B");
    }
}

class C
{
    public static void SomeStatic(A a)
    {
         // HERE IS WHERE POLYMORPHISM OCCUR
         a.a();
    }
}

C <中的多态性/ code>是因为 SomeStatic 方法可以使用对象引用引用B对象。如果通过引用 A A 调用它,则会调用方法。如果通过引用 B B 调用它,则会调用方法。这种在运行时更改被调用的实际方法的能力称为多态

Polymorphism in class C occurs because SomeStatic method could be call with a Reference to A object or a Reference to B object. If it's called with a reference to A, A's a method will be called. If it's called with a reference to B, B's a method will be called. This ability of changing, on runtime, the actual method being called is called Polymorphism.

重载几乎没有任何内容与多态性有关。实际上,如果您愿意,可以使用重载而不涉及任何继承。你甚至可以在没有面向对象的情况下进行重载重载只是让两个功能存在,但名称相同但参数不同。

Overloading barely has anything to do with Polymorphism. In fact, you can hace overloading with no inheritance involved if you want. You could even have overloading with no object orientation involved. Overloading is just letting two function to exist with the same name but with different parameters.

覆盖手,只是在专门(继承)类上重新定义一个方法。 覆盖一个方法是多态发生的必要条件。否则,运行时将没有 DUAL POSSIBILITIES (仔细查看示例)。

Overriding on the other hand, is just re-defining a method on a specialized (inherited) class. Overriding a method is necessary for polymorphism to happen. Otherwise, the would be no DUAL POSSIBILITIES on runtime (take a close look at the example).

C类是理解它的关键all:

Class C is the key to understand it all:

class Main
{
    public static void main(String[] args)
    {
        A a = new A();
        B b = new B();
        C.SomeStatic(a); // will call A's a
        C.SomeStatic(b); // will call B's a
        // AND THIS IS POLYMORPHISM
        // C will decide WHICH METHOD TO CALL 
        // only at runtime
    }
}

Poly :来自 greek 。意味着很多
变形:来自 greek 。意味着形式

Poly: comes from greek. Means many. Morph: comes from greek. Means form.

所以在多态中有很多 poly 形式变形)调用方法。将调用哪一个,取决于用于调用方法的对象。

So in Polymorphism there are "many" (poly) "forms" (morph) of calling a method. Which one will be called, depends on the object being used to call the method.

这篇关于多态性 - 重载/覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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