A very famous pit, the simple description is to call Super.get () in a method in subclass B of A. GetName (); Do you get A or B?
The answer is to know, B. Many of the statements on the Internet are reversed, such as the following example:
Import Java.util.date;public class Test extends date{public static void Main (string[] args) { new Test (). Test (); } public void Test () { System.out.println (Super.getclass (). GetName ());} }
<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " The > proves that the idea is that the GetClass () method is the final method of object and cannot be overridden for inheritance. This means that it can be inherited, but cannot be rewritten by itself. Then call GetClass () in the test class. GetName (); Get Test </span>
Class name, because it is not rewritten, so can only be inherited, that is, in Test call GetClass is equal to call super.getclass;
But I always felt that the explanation did not persuade me, and then went on to look at the information and found such an explanation:
/** * Returns the runtime class of this {@code Object}. The returned * {@code Class} object is the object, the locked by {@code * static Synchronized} methods of the R Epresented class. **/
This is a comment for the GetClass method in object, which means that the return
running theClass.
The interpretation states that to return this object runtime class, it is certainly not possible to refer to object itself, otherwise all classes call the GetClass () method to return Object.class
This is easier to understand. Because GetClass is native, that is, Java itself has been implemented, the purpose is very clear is to return the current running object class information, note is running, above the test object is running, of course, the return is the test class information.
A simple verification example is attached below:
public class Cat {public final void speak () {System.out.println ("the class===" +getclass (). GetName ());}}
public class RedCat extends cat{}
public class Smallredcat extends redcat{public void Test () {super.speak ();} public static void Main (string[] args) {smallredcat rc = new Smallredcat (); Rc.test (); RedCat RedCat = new RedCat (); Redcat.speak ();}}
The operating result is:
The Class===com.test.smallredcat
The Class===com.test.redcat
Reference: http://www.cnblogs.com/o-andy-o/archive/2012/12/22/2829563.html
Java Super.getclass Issues