简体   繁体   English

java中的动态多态如何应用于接口?

[英]How does dynamic polymorphism in java apply to interfaces?

I have been reading about dynamic polymorphism in java recently( I am beginner ). 我最近一直在阅读java中的动态多态性( 我是初学者 )。 As per my understanding if a reference of a parent class type is assigned as a reference to its child like below 根据我的理解, 如果父类类型的引用被指定为对其子代的引用,如下所示

tutorialspoint it involves dynamic polymorphism. tutorialspoint它涉及动态多态。 In the example discussed in the link, I understand an Object of employee class is used to access a (overridden) method of salary class. 在链接中讨论的示例中,我了解员工类的Object用于访问工资类的(重写)方法。 In that example neither is employee abstract nor is an interface. 在该示例中,既不是员工抽象也不是界面。 ( which means it is possible to create an object for the parent ). 这意味着可以为父级创建一个对象 )。

Now, as per this link stackoverflow , I see that an interface is used to discuss dynamic polymorphism. 现在,根据这个链接stackoverflow ,我看到一个接口用于讨论动态多态。

Question: How is it possible to use interface as an example for dynamic polymorphism? 问题:如何使用interface作为动态多态的示例?

Moreover, in the example discussed in tutorialspoint , it is said that compiler look for the method in parent class but JVM invokes the child class method during run time. 此外,在tutorialspoint中讨论的示例中,据说编译器在父类中查找方法,但JVM在运行时调用子类方法。

Interfaces neither have method definition nor can be instantiated, so how can 接口既没有方法定义也没有实例化,所以怎么可以
List<Animal> animalPen = new LinkedList<>(); be used for dynamic polymorphism. 用于动态多态。

Actually, the explanation isn't really much different. 实际上,解释并没有太大的不同。

List animalPen = new LinkedList<>();
boolean empty = animalPen.isEmpty();

In this example, the compiler validates whether animalPen has a method isEmpty by looking to its reference type List . 在此示例中,编译器通过查看其引用类型List来验证animalPen是否具有方法isEmpty List declares the method isEmpty and so, even if it does not define it, the system is then guaranteed that animalPen has a defined method by that signature. List声明方法isEmpty ,因此,即使它没有定义它,系统也可以保证animalPen具有该签名的已定义方法。

This is because a non-abstract class must define all methods from all interfaces it implements. 这是因为非抽象类必须从它实现的所有接口定义所有方法。 This ensures that all instances of an interface is one which has, somewhere in its hierarchy, defined the interface's methods. 这可以确保接口的所有实例都是在其层次结构中的某个位置定义接口方法的实例。

You use interfaces to do polymorphism when you have different behaviours in your objects. 当对象中有不同的行为时,可以使用接口来执行多态操作。 Let's say you have a class Duck and you have a FlyBehavior variable declared. 假设您有一个Duck类,并且您声明了一个FlyBehavior变量。 In this case the first thing you think of doing is a class named FlyBehavior to make an object of that type. 在这种情况下,您首先想到的是一个名为FlyBehavior的类来创建该类型的对象。 Now let's say you have different type of ducks, like the Mallard duck, Redhead duck and now you have a Rubber duck, all of them extend the Duck class. 现在让我们假设你有不同类型的鸭子,如野鸭,红头鸭,现在你有一只橡皮鸭,它们都延伸了鸭子类。 Your rubber duck will not fly, so FlyBehavior will be different for the rubber duck. 你的橡皮鸭不会飞,所以FlyBehavior对于橡皮鸭会有所不同。 So, you make FlyBehavior an interface and create two new classes: ItFlies and NoFly, both implement the FlyBehavior interface. 因此,您将FlyBehavior作为一个接口并创建两个新类:ItFlies和NoFly,它们都实现了FlyBehavior接口。 The constructor in Duck will have aa FlyBehavior parameter that you'll need to fill when you create a new object of type Duck, as you say, an interface can't be instantiated, but since ItFlies and NoFly, both implement the FlyBehavior interface, you can fill the FlyBehavior parameter with these two classes (or any class that implements FlyBehavior). Duck中的构造函数将具有一个FlyBehavior参数,当您创建Duck类型的新对象时,您需要填充该参数,正如您所说,接口无法实例化,但是由于ItFlies和NoFly都实现了FlyBehavior接口,您可以使用这两个类(或任何实现FlyBehavior的类)填充FlyBehavior参数。 This object oriented technique is also useful to make your program more independent and flexible in case of making modifications. 这种面向对象的技术也可以使您的程序在进行修改时更加独立和灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM