阐述多态 [英] Elaborating on Polymorphism

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

问题描述

我已经阅读了许多有关多态性的stackoverflow问题.问题是构建我的想法的问题,因为我已经阅读并研究了很多东西,以至于我想到了很多想法.我需要对我关于多态性的思想进行评论和批评.我将使用Java演示我的想法.

I have read many questions on stackoverflow about polymorphism. The question is a question of structuring my ideas because I have read and researched a lot to the point where I have many ideas in mind. I need comments and criticism about my thoughts on polymorphism. I will use Java to demonstrate my ideas.

多态性是在谈论某个实体时具有不同形式的特征.可以通过以下方式实现:

Polymorphism is a characteristic of having different forms when talking about a certain entity. It could be achieved in the following ways:

  • 派生类从其基类继承所有可见方法,因此可以override某些方法的行为. 例如:

  • A derived class inherits from its base class all the visible methods and accordingly could override the behaviour of certain methods. e.g:

class Shape {
   void draw(){
         System.out.println("I am drawing using a shape object");
   }
}

class Rectangle extends Shape{
   void draw(){
     // This method is overriden
     System.out.println("I am drawing using a rectangle object");
   }
}

此处实现多态的方法是,如果在运行时将draw()Rectangle的实例一起使用,则会调用Rectangle类中的draw()的实现.

The way polymorphism is achieved here is that if at run-time, draw() was used with an instance of Rectangle the implementation of draw() in the Rectangle class would be invoked.

由于矩形是形状,因此多态性是draw()根据调用上下文的不同而采用不同形式的事实.这也称为继承多态性.

Since a rectangle is a shape, polymorphism is the fact that draw() took different forms depending on the context where it was being called. This is also known as inheritance polymorphism.

  • 实现多态的另一种方法是通过接口.

  • Another way to achieve polymorphism is through interfaces.

interface Vehicle{
     void move();
}

class Car implements Vehicle{
     void move(){
         System.out.println("The car is moving!");
     }
}

class Plane implements Vehicle{
     void move(){
         System.out.println("The plane is flying!");
     }
}

// we declare an object of type Vehicle
// At compile-time, the concrete implementation of someVehicle is no known  
// and thus polymorphism is achieved in the sense that someVehicle could
// either be a Car or a Plane depending on which concrete class is used 
// to instantiate it at run-time.

Vehicle someVehicle;

  • 用户定义的运算符重载(不是Java,而是C ++)

  • User-defined operator overloading(Not in Java, but in C++)

    在Java中,运算符重载的一个示例是+运算符,其中取决于操作数,+执行某些功能.如果将+应用于字符串,则将它们连接起来.如果将其应用于整数,则会进行算术加法.

    In Java, an example of operator overloading would be the + operator where depending on the operands, the + performs a certain functionality. If + is being applied to strings, it concatenates them. If it is being applied to integers, it does arithmetic addition.

    现在,方法重载也是实现多态的一种方法吗?我对多态性的了解错过了什么?

    Now, is method overloading also a way to achieve polymorphism? What did I miss in my understanding of polymorphism?

    推荐答案

    "多态"一词表示许多形式" .

    您给出的两个例子是绝对有效的多态例子:

    The two examples that you give are definitively valid examples of polymorphism:

    • 在第一个示例中,您具有绘制"功能的多种形式",具体取决于您在其上调用绘制"的对象是哪种形状

    • In the first example you have "many forms" of the "draw" functionality depending on what kind of Shape is the object you call "draw" on

    在第二种形式中,根据车辆的不同,您具有移动"功能的多种形式"

    In the second one you have "many forms" of the "move" functionality depending on the vehicle

    由于后期绑定,将在运行时确定将要调用的实际方法.

    The actual method that will be invoked is decided at run time due to late binding.

    实际上,两个示例都非常相似,它们都是 subtyping 的示例,这是您在Java中实现多态的方式.

    In fact, both examples are quite similar and they are both examples of subtyping which is the way you implement polymorphism in Java.

    请注意,在Java中需要子类型化,因为Java是一种静态类型的语言.在像Smalltalk这样的动态类型语言中,即使对象的类不属于同一类层次结构,也可以使用不同的"draw"方法实现就足够了.

    Note that subtyping is needed in Java cause Java is a statically typed language. In dynamically typed languages like Smalltalk, having different implementations of the "draw" method would be enough, even if the objects' classes did not belong to the same class hierarchy.

    现在,关于方法重载:的确,如果您定义了一个接收不同参数类型的方法的多个版本,则该方法也将具有许多形式".

    Now, about method overloading: it's true that if you define several versions of a method receiving different parameter types you'd also have "many forms" of that method.

    区别在于,在这种情况下,要在编译时决定调用哪种方法.这就是为什么许多人将方法重载称为静态多态性.

    The difference is that in that case, the decision of which method to call is made at compile-time. That's why many people refer to method overloading as static polymorphism.

    但是,由于它是静态的,并且您可以通过为方法赋予不同的名称并根据所使用的类型来确定要调用的是哪种方法而基本实现同一件事,所以许多人也会说该方法重载实际上不是多态.

    However, due to the fact that it's static and that you could basically achieve the same thing by giving different names to the methods and deciding yourself which one to call based on the types you are using, many people would also say that method overloading is not actually polymorphism.

    我认为这基本上取决于您要使用的多态性"的定义,但我希望以上解释有助于弄清差异.

    I think it basically depends on the definition of "polymorphism" you want to use, but I hope the above explanation helps clarify the difference.

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

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