多态性 - 重载/覆盖

发布于 2024-10-17 03:27:08 字数 553 浏览 3 评论 0原文

我知道这个问题已经在 StackOverflow 上被解决了,而且已经有很多关于这个问题的问题了。 我可能已经阅读了其中的每一本,但仍然存在一个棘手的疑问:我认为我非常了解重载和重写。让我着迷的是多态性。

例如,这个问题的公认答案用 shape.Draw().我很困惑这与覆盖有何不同(其他时候我很困惑它与重载有何不同)。

另外 - 多态本质上意味着从抽象类派生吗? (我认为我读过的关于该主题的几乎所有答案都使用抽象动物类,并使猫和狗喵喵/吠叫:)

总结,我的问题是:

  1. 什么多态性是关于重载和重写吗?

  2. 有人可以解释一下没有抽象类的多态性吗 - 谢谢!

  3. 重载/重写不是多态性的子类型,不是吗?

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

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.

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 sum up, my questions are:

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

涙—继续流 2024-10-24 03:27:08

回答您的问题:

  1. 它能够根据用于调用它的对象在运行时中选择更专门的方法。
  2. 当然。多态性可以在不涉及抽象类的情况下发生。
  3. 不,重载/重写不是类型的多态性。

这是一个不涉及抽象类的多态性示例。

// 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 类中出现多态性是因为可以通过对 A 对象的引用对 B 对象的引用来调用 SomeStatic 方法>。如果通过对 A 的引用来调用它,则将调用 Aa 方法。如果通过引用 B 来调用它,则将调用 Ba 方法。这种在运行时改变被调用的实际方法的能力称为多态性

重载多态性几乎没有任何关系。事实上,如果您愿意,您可以进行重载而不涉及继承。您甚至可以进行重载而不涉及面向对象重载只是让两个函数具有相同的名称但具有不同的参数。

另一方面,重写只是在专门(继承)类上重新定义方法。 重写方法对于多态性的发生是必要的。否则,运行时将没有双重可能性(仔细查看示例)。

C类是理解这一切的关键:

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:来自希腊语。意思是许多
Morph:来自希腊语。意思是形式

因此,在多态中,有“许多”“形式”变形 >) 调用方法。将调用哪一个取决于用于调用该方法的对象。

To answer your questions:

  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();
    }
}

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.

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

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: comes from greek. Means many.
Morph: comes from greek. Means form.

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.

岛徒 2024-10-24 03:27:08

实际上,除了重载和重写之外,多态性并不是单独的东西。

重载和重写都是一种特定类型的多态性:

  • 重载称为临时多态性。
  • 重写用于面向对象的类型多态性,以在子类上实现不同的行为。

Actually polymorpishm is not something separate beside of overloading and overriding.

Both - overloading and overriding - are a specific type of polymorphism:

  • Overloading is referred to as adhoc-polymorphism.
  • Overriding is used in object orientation for type-polymorphism to implement different behaviour on subclasses.
情徒 2024-10-24 03:27:08

多态性是一个 OOP 概念,其中单个任务可以通过多种方式执行。例如。您可以通过借记卡、网上银行支付账单。

它有两种类型:

1)编译时多态性:这会导致静态绑定(编译器用来匹配签名的签名匹配技术)。运算符重载、函数重载实际上是实现编译时多态性的一种方式。

2) 运行时多态性:导致动态绑定。重写(在后续子类中重新定义方法)是实现运行时多态性的一种方法。

Polymorphism is an OOP concept in which a single task is performed in many ways. Eg. You can pay bill through debit card , through netbanking.

It is of two type :

1) Compile time polymorphism : which causes static binding(A signature matching technique that compiler use to match the signature). Operator overloading , function overloading is actually a way to implement compile time polymorphism.

2) Run time polymorphism : which causes dynamic binding. Overriding(redefining a method in subsequent sub classes) is a way of implementing the run time polymorphism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文