简体   繁体   English

java编译器如何知道继承的方法?

[英]How does the java compiler know of inherited methods?

We use inheritance in Java to abstract out similar behavior in a superclass and let all sub classes inherit it. 我们在Java中使用继承来抽象出超类中的类似行为,并让所有子类继承它。 One of the advantages of this is that , we now have only one copy of the method to maintain (ie in the superclass). 这样做的一个优点是,我们现在只有一个要维护的方法副本(即在超类中)。

Class Animal
{
   public void makeNoise()
   {

   }

   public void sleep()
   {

   }   
} 

Class Cat extends Animal
{
     // Override the makeNoise method
     public void makeNoise()
     {

     }
}

Class someClass
{
     public static void main(String args[])
     {
          Cat fluffy = new Cat();

          fluffy.sleep();
     }
}

I am trying to understand how the Java compiler knows of the sleep() method for a Cat type reference. 我试图了解Java编译器如何知道Cat类型引用的sleep()方法。 There can't be a copy of the method in the Cat subclass (it defeats the purpose of having it in the superclass and letting all subclasses inherit from it). Cat子类中不能有该方法的副本(它违背了在超类中使用它并让所有子类继承它的目的)。 Is this information stored in some other place ? 这些信息是否存储在其他地方?

When the compiler sees fluffy.sleep() it first looks in the Cat class for a public instance method called sleep that takes no parameters. 当编译器看到fluffy.sleep()它首先在Cat类中查找名为sleep的公共实例方法,该方法不带参数。 Since it doesn't find it, it moves up the inheritance chain to Animal , and does the same check on Animal . 由于没有找到它,它移动起来继承链Animal ,并不会在同一检查Animal It finds it there, so all is good. 它找到了它,所以一切都很好。

This information isn't really "stored" anywhere except in the code, and then the Java byte code. 除了代码之外,这些信息并没有真正“存储”在任何地方,然后是Java字节代码。

In case of interface we are able to call the Object class methods on them without interface extending Object. 在接口的情况下,我们能够在没有接口扩展Object的情况下调用它们上的Object类方法。 For example:- 例如:-

public interface Test { 
}

public class MyClass extends Object implements Test {

public static void main() {
Test test = new MyClass();
test.hashCode();
// You can call Object Class methods on Test interface and Test interface
//does not extends Object.

}

//In Java Specification 9.2:- If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. //在Java Specification 9.2中: - 如果接口没有直接的超接口,那么接口隐式声明一个公共抽象成员方法m,其中包含签名s,返回类型r和throws子句t,对应于带有签名s的每个公共实例方法m,返回类型r,并在Object中声明throws子句t,除非接口显式声明具有相同签名,相同返回类型和兼容throws子句的抽象方法。 It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object. 如果接口在将对象声明为m的情况下显式声明了这样的方法m,那么这是一个编译时错误。

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

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