天天看點

Chapter 7.Better Living in ObjectvilleQuestion問題

Question

  • The JVM starts walking up the inheritance tree,

    starting at the class type you invoked the method on. But what

    happens if the JVM doesn’t ever find a match?

    • The compiler guarantees that a particular method is callable for a specific reference type,but it doesn’t say (or care) from which class that method actually comes from at runtime.Remember that if a class inherits a method, it has the method.Where the inherited method is defined makes no difference to the compiler.But at runtime, the JVM will always pick the right one. And the right one means, the most specific version for that particular object.

問題

  • 虛拟機從你調用方法的類開始向上搜尋繼承樹。當JVM沒有找到會發生什麼?
    • 編譯器通過特别的引用類型來保證特定的方法會被調用,在運作時它不會在乎該方法實際上在哪個類找到的。記住,如果類繼承一個方法,它便有了那個方法。該方法在哪裡被聲明對于編譯器沒有差別。但在運作時,JVM總是會找到正确的方法。正确的方法意思是最接近該類型的版本。