多态与继承 [英] Polymorphism vs Inheritance

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

问题描述

假设我有两个班级:动物和狗.狗是动物的子类.我执行以下代码:

Suppose I have two classes: Animal and Dog. Dog is a subclass of Animal. I do the following code:

Animal a = new Dog();

现在,我可以通过a变量来调用Dog类的方法.

Now I can call methods of the Dog class through the a variable.

但是我的问题是:如果我可以通过Dog对象(继承性)调用Animal的所有方法,那为什么我应该使用多态原理呢?我可以声明:

But my question is this: if I can call all of Animal's methods through the Dog objects (inheritance) than why should I use the polymorphism principle? I can just declare:

Dog d = new Dog();

通过此声明,可以使用Animal的所有方法和Dog方法.那么为什么要使用多态呢?非常感谢您的回答.

With this declaration can use all of Animal's methods and Dog methods. So why use polymorphism? Thank you very much for your answer.

推荐答案

在Java中,多态和继承的概念被焊接在一起".通常,不必一定是这样:

In Java, the concepts of polymorphism and inheritance are "welded together"; in general, it does not have to be that way:

  • 多态性使您可以在不知道类的确切类型的情况下调用类的方法
  • 继承使派生类共享其基类的接口和代码

在某些语言中,继承与多态性脱钩:

There are languages where inheritance is decoupled from polymorphism:

  • 在C ++中,您可以继承一个类而不会产生多态行为(即,不要使用virtual标记基类中的函数)
  • 在Objective C中,您可以在不相关的类上实现一个方法,然后在仅知道该方法签名的地方调用它.
  • In C++ you can inherit a class without producing polymorphic behavior (i.e. do not mark functions in the base class with virtual)
  • In Objective C you can implement a method on an unrelated class, and call it from a place that knows only the signature of the method.

回到Java,使用多态性的原因是将您的代码与实现其对手方的细节脱钩:例如,如果您可以编写一种适用于各种动物的方法Feed(Animal animal),当您添加Animal的更多子类或实现时,该方法将仍然适用.这与Feed(Dog dog)方法相反,后者会与狗紧密结合.

Going back to Java, the reason to use polymorphism is decoupling your code from the details of the implementation of its counter-parties: for example, if you can write a method Feed(Animal animal) that works for all sorts of animals, the method would remain applicable when you add more subclasses or implementations of the Animal. This is in contrast to a Feed(Dog dog) method, that would be tightly coupled to dogs.

Dog d = new Dog();

声明是肯定的,如果您知道方法的其余部分专门针对狗,则没有一般的理由可以避免这种情况.但是,在许多情况下情况并非如此:例如,您的类或方法通常对确切的实现不敏感,例如

declaration goes, there is no general reason to avoid this if you know that the rest of your method deals specifically with dogs. However, in many cases the later is not the case: for example, your class or your methods would often be insensitive to the exact implementation, for example

List<Integer> numbers = new ArrayList<Integer>();

在这种情况下,您可以将new ArrayList<Integer>()替换为new LinkedList<Integer>(),并知道您的代码将要编译.相反,如果您的numbers列表被声明为ArrayList<Integer> numbers,则这种转换可能不是确定的.

In cases like that, you can replace new ArrayList<Integer>() with new LinkedList<Integer>(), and know that your code is going to compile. In contrast, had your numbers list been declared as ArrayList<Integer> numbers, such switchover may not have been a certainty.

这称为接口编程".在Stack Overflow上有一个很好的答案对此进行解释.

This is called "programming to an interface". There is a very good answer on Stack Overflow explaining it.

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

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