简体   繁体   English

Java接口和继承与多态

[英]Java Interfaces and Inheritance and Polymorphism

To conceptually understand inheritance, interfaces & polymorphism, I created a couple of classes to test things. 为了概念上理解继承,接口和多态,我创建了几个类来测试事物。 The result I am getting is not the one I expected. 我得到的结果不是我预期的结果。 The code is as follows: 代码如下:

public class Animal implements Comparable{
//Animal constructor & methods
public int compareTo(Object arg0){System.out.println("Animal.compareTo");}
}

public class Bear extends Animal{
//Bear constructor & methods
}

public class PolarBear extends Bear implements Comparable{
//PolarBear constructor & methods
public int compareTo(Object arg0) {System.out.println("PolarBear.compareTo");

I don't get any error saying that I can't implement Comparable again. 我没有得到任何错误,说我无法再次实现Comparable。 But in my testing, I can't get back to Animal's compareTo method once I create a PolarBear, even though PolarBear should inherit the Animal's compareTo. 但是在我的测试中,一旦我创建了PolarBear,我就无法回到Animal的compareTo方法,即使PolarBear应该继承Animal的compareTo。 I tested the following: 我测试了以下内容:

Animal bobo = new PolarBear();
bobo.compareTo(null);
((PolarBear) bobo).compareTo(null);
((Animal)bobo).compareTo(null);
((Comparable) bobo).compareTo(null);    

And each of them printed: 并且他们每个人印刷:

 PolarBear.compareTo

Is there any way to access the Animal 's compareTo ? 有没有办法访问AnimalcompareTo Logically, wouldn't I want the ability to be able to compare the animal qualities of a PolarBear , as well as the PolarBear qualities? 从逻辑上讲,我不希望能够比较PolarBear的动物品质以及PolarBear品质吗?

It doesn't matter that you are casting your bobo object to PolarBear , Animal , or Comparable , the run-time type of the object will always be PolarBear since you instantiated it as bobo对象转换为PolarBearAnimalComparable并不重要,对象的运行时类型将始终为PolarBear因为您将其实例化为

Animal bobo = new PolarBear();

Therefore whenever you call .compareTo(wtv) it'll call the PolarBear implementation. 因此,无论何时调用.compareTo(wtv)它都会调用PolarBear实现。

This is what Polymorphism is. 这就是多态性。 Read up on late binding as that's how polymorphishm is implemented in java. 阅读后期绑定,因为这是在java中实现polymorphishm的方式。

If you want to call the compareTo(wtv) method of your parent classes, then you have to do super.compareTo(wtv) inside any other subclass' methods. 如果要调用父类的compareTo(wtv)方法,则必须在任何其他子类的方法中执行super.compareTo(wtv)

compareTo and inheritence is generally a tricky business . compareTo和inheritence通常是一个棘手的业务 in theory, having subclasses to a Comparable concrete superclass breaks the comparable contract. 理论上,拥有可比较的具体超类的子类会打破可比较的合同。 for example - suppose i have a superclass A that compares to other A's by checking fields a1 and a1. 例如 - 假设我有一个超类A,它通过检查字段a1和a1与其他A进行比较。 now lets add a subclass B that has an extra field b1. 现在让我们添加一个具有额外字段b1的子类B. anA.compareTo(aB) would compare by a1 and a2 while ((A)aB).compareTo(anA) would not do the same anA.compareTo(aB)将比较a1和a2而((A)aB).compareTo(anA)不会做同样的事情
as to your question - you can call the Animal version of compareTo only from the subclass itself, by doing something like 至于你的问题 - 你可以通过做类似的事情从子类本身调用compareTo的Animal版本

super.compareTo(<something>)

but not from "outside", as you've overridden the method. 但不是来自“外部”,因为你已经覆盖了这个方法。 it is your responsibility to maintain the same "contract" to the outside world as the method you've just overridden. 您有责任与您刚刚覆盖的方法保持与外界相同的“合同”。

您始终可以从PolarBear中调用super.compareTo(arg0)来调用Animal.compareTo中的逻辑。

It's very simple. 这很简单。 the underlying object is of PolarBear class and you are changing the interface (Class that is used to hold the reference of the object) of the object every time. 底层对象是PolarBear类,您每次都要更改对象的接口(用于保存对象引用的Class)。

JVM check at compile time whether a method in the interface (Class which is used to hold the reference of the object) exist or not but binding takes place at run time because Child (PolearBear) has that method so it will always run that. JVM在编译时检查接口中的方法(用于保存对象引用的类)是否存在,但是绑定是在运行时进行的,因为Child(PolearBear)具有该方法,因此它将始终运行该方法。

use super.compareTo() in order to call the parent method. 使用super.compareTo()来调用父方法。

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

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