GetClass (), class attributes, and GetName () parsing (finishing)

Source: Internet
Author: User

One,

GetClass Method:
Type: public final class<? Extends Object> getclass ()
Function: Returns the Java.lang.Class object of the Run-time class of the object (interpretation on the API)
There is a method type to know that the method can only be invoked by an instance variable of the class
Example:

JButton B1 = new JButton ("Button1"); System.out.println (B1.getclass ());
Output:
Class Javax.swing.JButton

Class attribute
When you want to get a class object (as a function argument), you can't call the GetClass method, you can only use the class name. class to achieve the effect
Example:

System.out.println (Jbutton.class);

Output:
Class Javax.swing.JButton

GetName method:
Type: Public String getName ()
Function: Returns the entity name represented by the secondary class object as a string
Example:

JButton B1 = new JButton ("Button1"); System.out.println (B1.getname ());
Output:
Javax.swing.JButton

It can be found that the class attribute and the output returned by the GetClass are the same, with GetName returned with a class and a space less than the preceding two kinds.

. The Eclipse tool can press "." And then immediately prompts you for a choice.
So how does he know "." What are the ways to do it later?
The grammar he uses is GetClass (). GetMethods ();

Second,

. class is actually loaded in the Java runtime

GetClass () is dynamically loaded when the program is run

The following examples illustrate:
First build a base class baseclass      package  classyongfa;      public  class  Baseclass {      Private  String height;   public  string  getheight ()    {      return  height;  }       Public  void  setheight (string height)    {           this .height=height;       }   Following is the inheritance BaseClass class extendclass      package  classyongfa;      Public  class  extendclass extends  Baseclass {      Private  string  width;   public  string getwidth ()    {      return  width;  }      public  void  setwidth (string width)    {           this .width=width;       }    Public  static  void  main (string[] arg0)    {       Baseclass baseclass1=new  extendclass ();      baseclass baseclass2=new  baseclass ();      system.out.println (Baseclass1.getclass (). GetSimpleName ());// The actual operation is inherited class Extendclass       System.out.println (Baseclass2.getclass (). Getsimplename ()); The actual operation is BaseClass             system.out.println (baseclass.class. Getsimplename ());//Load Time class name    System.out.println (extendclass.class. Getsimplename ());//Load time class name      }      Results       extendclass   baseclass   baseclass   Extendclass

Three, four ways to get class objects Java reflection mechanism

Here is a concrete example to illustrate. This example comes from the "proficient in Hibernate 3.0 Java Database Persistence Layer Development practice" book.

First build a file under the Com.hqh.reflect Useinfojava

Package com.hqh.reflect;

public class Useinfo {

Private Integer ID;
Private String UserName;
private String password;

Public Integer getId () {
return ID;
}

public void SetId (Integer id) {
This.id = ID;
}

Public String GetUserName () {
return userName;
}

public void Setusername (String userName) {
This.username = UserName;
}

Public String GetPassword () {
return password;
}

public void SetPassword (String password) {
This.password = password;
}

}

Package com.hqh.reflect;

public class Getclasstest {

public static void Main (string[] args) {
Getclasstest test = new Getclasstest ();
if (test. Classcheck ())
System.out.println ("OK");

}

public Boolean Classcheck () {
try {
SYSTEM.OUT.PRINTLN ("Obtaining objects through the class itself");
Class userclass = This.getclass ();
System.out.println (Userclass.getname ());
System.out.println ("===========");

System.out.println ("Get the parent class object from an instance of a subclass");
Useinfo useinfo = new Useinfo ();
UserClass = Useinfo.getclass ();
System.out.println (Userclass.getname ());
Class Subuserclass = Userclass.getsuperclass ();
System.out.println (Subuserclass.getname ());
System.out.println ("===========");

System.out.println ("Get Objects by class name. Class");
Class forclass = Com.hqh.reflect.UseInfo.class;
System.out.println (Forclass.getname ());
System.out.println ("===========");

SYSTEM.OUT.PRINTLN ("Fetch object by string of class name");
Class forname = Class.forName ("Com.hqh.reflect.UseInfo");
System.out.println (Forname.getname ());
System.out.println ("=============");

catch (Exception e) {
E.printstacktrace ();
return false;
}
return true;
}
}


Results:

Get objects from the class itself
Com.hqh.reflect.GetClassTest
===========
Gets the parent class object from an instance of a subclass
Com.hqh.reflect.UseInfo
Java.lang.Object
===========
Gets the object
Com.hqh.reflect.UseInfo
===========
through a string of class names.
Com.hqh.reflect.UseInfo
=============
OK

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.