Tag:port math import color code Object Type interview end test
What is the output of the program below? 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 ()); } }
In the test method, the GetClass (). GetName () method is called directly, and the test class name is returned. Because GetClass () is defined as final in the object class, the subclass cannot overwrite the method, so the GetClass (). GetName () method is called in the test method, which is actually called the GetClass () method inherited from the parent class. is equivalent to calling the Super.getclass (). GetName () method, so the Super.getclass (). GetName () method returned should also be test.you might think of date, but the actual result is test. Yes, you're not mistaken, super.getclass () does not return a reference to a superclass. Let's do another experiment, calling the GetClass (). GetName () method directly in the test method, and the result is test. Why doesn't super play a role? Simply put, super does not represent a reference to a superclass. Because Super does not have the ability to represent a reference to a superclass, it simply represents the method that invokes the parent class. Therefore, in a subclass method, you cannot use SYSTEM.OUT.PRINTLN (super) or Super.super.mathod (); In fact, Super.getclass () is the method that represents the invocation of the parent class. The GetClass method comes from the object class, which returns the type of objects at run time. Because the object type at run time is test, both This.getclass () and Super.getclass () are returned as test. In additionbecause GetClass () is defined as final in the object class, the subclass cannot overwrite the method, so the GetClass (). GetName () method is called in the test method, which is actually called the GetClass () method inherited from the parent class. is equivalent to calling the Super.getclass (). GetName () method, so the Super.getclass (). GetName () method returned should also be Test. If you want the name of the parent class, you should use the following code:Java codegetclass (). Getsuperclass (). GetName ();
Java face question Super.getclass () method call