Explanations of GetClass () and Getsuperclass () in Java

Source: Internet
Author: User

To explain the problem, let's take a look at the following code:

Package Com.yonyou.test;import Java.util.date;class Test extends date{private static final long Serialversionuid = 1l;pub Lic static void Main (string[] args) {new Test (). print ();}   public void print () {System.out.println ("The name of the currently running class is:" +super.getclass (). GetName ()); System.out.println ("The name of the current running class is:" +this.getclass (). GetName ()); System.out.println ("The name of the inherited parent class for the current run class is:" +this.getclass (). Getsuperclass (). GetName ());}}

What does the output of the above code look like?
Perhaps you need to be on the machine to debug, because some are not sure, below we will analyze together:

After commissioning the machine, the results of the operation are as follows:

The name of the current running class is: Com.yonyou.test.Test
The name of the current running class is: Com.yonyou.test.Test
The name of the inherited parent class for the currently running class is: Java.util.Date

Let's start by analyzing what GetClass () is returning:

Card JDK source code can see the following:

/**
* Returns the runtime class of this {@code Object}. The returned
* {@code Class} object is the object this is locked by {@code
* static Synchronized} methods of the R Epresented class.
*
* <p><b>the Actual result type is {@code class<? extends | X|>}
* Where {@code | X|} Is the erasure of the static type of the
* expression on which {@code getclass} is called.</b> for
* Example , no cast is required in this code FRAGMENT:</P>
*
* <P>
* {@code number n = 0;} <br>
* {@code class<? extends number> C = N.getclass ();}
* </P>
*
* @return the {@code Class} object, represents the runtime
* Class of this object.
* @see <a href= "http://java.sun.com/docs/books/jls/" >the Java
* Language specification, Third Edition (15.8.2 C Lass
* literals) </a>
*/
Public final native class<?> GetClass ();

Be sure to look at the English comment in the blue section of the comment above, meaning that the corresponding object for the class that is currently running is returned, and that super can be understood as the parent of test date,

So, who is the object of the current date class running? Yes, that's its subclass test, so This.getclass (0.getName () and Super.getclass (). GetName () returns the

It's all com.yonyou.test.Test.

Then look at the source code of Getsuperclass ()

/**
* Returns The <code>Class</code> representing the superclass of the entity
* (class, interface, primitive type or void) represented by this
* <CODE>CLASS</CODE>. If This <code>Class</code> represents either the
* <code>Object</code> class, an interface, a primitive type, or void, then
* NULL is returned. If This object represents a array class then the
* <code>Class</code> object representing the <code>Object</code> Class is
* Returned.
*
* @return The superclass of the class represented by this object.
*/
public native class<? Super T> Getsuperclass ();

Yes, the blue one here is the parent class that returns the current entity class. So to return the parent class of the current class, use the following method

Super.getclass (). Getsuperclass (). GetName ();

Explanations of GetClass () and Getsuperclass () in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.