多态与抽象:多态是实现抽象的方法之一吗? [英] Polymorphism vs Abstraction : Is polymorphism one of the way to achieve Abstraction?

查看:158
本文介绍了多态与抽象:多态是实现抽象的方法之一吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对多态的理解是,它是实现抽象的方法之一?每个人都同意我的观点吗?还是我的想法有缺陷?

My understanding of a Polymorphism is, it is one of the way of achieving Abstraction ? Does every one agree with my view ? Or there is any flaw in my thinking ?

推荐答案

多态

这是处理以更有效方式做出不同反应的对象的能力。不同的对象反应不同,但是共享相同的方法。这些方法可以在一个公共界面中找到。

This is the ability to work with objects that react differently in a more efficient manner. The different object react differently, yet share the same methods. These methods are found in one common interface.

例如,如果我们使用不同类型的鸭子,我们可以说它们都有共同的功能,但是用略有不同的方式来做。

For example if we were working with different types of ducks, we could say that they all have common things that they do, but do them in slightly different ways.

Duck[] ducks = new Duck[3];

    ducks[0] = new MallardDuck();
    ducks[1] = new RedheadDuck();
    ducks[2] = new RubberDuck();

    for(int i=0; i < ducks.length; i++) {
        System.out.println("Duck #" + (i + 1) + ":\n---------------------");
        ducks[i].quack();
        ducks[i].display();
        System.out.println(ducks[i].toString() + Driver.BLANK_LINE);
    }




输出:

OUTPUT:



Duck #1:
---------------------
Quack
Drawing a Mallard Duck to screen
MallardDuck@15db9742

Duck #2:
---------------------
Quack
Drawing a Red Headed Duck to screen
RedheadDuck@6d06d69c

Duck #3:
---------------------
Squeak!
Drawing a Rubber Duck to screen
RubberDuck@7852e922

围绕鸭子类型的接口鸭子,使程序员可以调用for循环,在其中可以调用鸭子接口中包含的任何方法。

The use of an interface "Duck" around the types of ducks, allows a programmer to call a for loop in which they can call any method that is contained in the "Duck" interface.

抽象

现在您可能会认为在构建接口的情况下抽象和多态性是相似的。以 Duck界面为例:

Now where you may be thinking Abstraction and Polymorphism are similar might be in the case where you build your interface. Take the "Duck" interface for example:

    public interface Duck {

    public abstract void display();

    public abstract void performQuack();


}

您可以看到它是抽象的随着它。真正的接口不提供任何指令。它只是创建一组需要在实现它的每个类中的通用方法。这样,您可以拥有许多相似的事物,它们的反应不同,遵循一组常见的抽象常见方法。

You can see it is about as abstract as it gets. A true interface does not give any directives. It simply creates a set of common methods that needs to be in each class that implements it. In this way you can have many similar things, that react differently, follow a common "abstract" set of common methods.

可能有一种不太抽象的方法控制你的鸭子。您可以将其设置为抽象类,而不是将其设置为接口。

It is possible to have a less abstract way to control your ducks. Instead of setting it as an "interface", you can set it as an "abstract class".

    public abstract class Duck {

    public abstract void display();

    public abstract void performQuack();

        public void swim() {
        System.out.println("All ducks float, even decoys!");
    }

}

在本课程中,游泳方法已添加。该方法包含静态反应。 游泳类是脊形的。可以重写它,但是无论如何,它甚至不能与此类中其他方法的抽象程度相比。

In this class The "swim" method was added. This method contains a static reaction. The "swim" class is ridged. It can be overridden, but none the less, it does not even compare to how abstract the other methods are in this class.

为什么这么僵化?显而易见的答案将是输出的字符串。仔细观察,它的输出方式(到控制台)使其更加僵化。

Why is it rigid? The obvious answer would be the String that gets outputted. Looking even closer, the way it get outputted (to console) makes it even more rigid.

如果您的程序不支持控制台怎么办?

What if your program doesn't support a console?

这就是为什么抽象很重要的原因。

This is why Abstraction is important.

结论

从某种程度上讲,您在正确的道路上。您不需要抽象就可以实现多态。然而,您的好奇心正在走向正确。没有抽象,就没有使对象变为多态的意义。如果每个对象都做完全相同的事情,则这些对象不需要分开。他们只是需要是同一对象的单独实例。

To a slight degree you are on the right track. You do not need to have abstraction to achieve polymorphism. Yet, where your curiosity is heading is correct. Without abstraction there really is no point of making objects polymorphic. If every object does the exact same thing, then those objects do not need to be separate. they just need to be separate instances of the same object.

我希望这有助于弄清多态与抽象之间的区别。

I hope this helps to clarify the difference between Polymorphism and Abstraction.

这篇关于多态与抽象:多态是实现抽象的方法之一吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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