Java-reflect (Reflective) 1

Source: Internet
Author: User

Java-reflect Special Topic (reflection)

· The use of class classes
• Reflection of the method
• Reflection of Member variables
• Reflection of the constructor function
· Java class loading mechanism


1 Use of Class
1) In an object-oriented world, everything is object.
In the Java language, static members, ordinary data type classes are not objects?
Whose object is the class?
Class is an object, and the class is an instance object of the Java.lang.Class class instance object
There is a class named class

2) What does this class say?
Comment by Me:class->foo (instance of class instance)->foo1 (instance of Foo instance), Class
Class foo{}
& How the instance object of Nbsp; //foo represents
  foo foo1 = new Foo ();//foo1 is the expression.
  //foo This class is also an instance object, how to represent an instance object of class,
  //any class is an instance object of class, there are three representations of this instance object
   
  //the first way---> actually tells us that any class has an implied static member variable class
  class c1 = foo.class;
  
  //The second expression   already know the object of the class through the GetClass method
  class c2 = Foo1.getclass ();
  
  /* official website C1, C2 represents the class type of the Foo class (class type)
   * Everything is Object,
   * Class is also an object, is an instance object of Class
   * This object we call the class type of the Class
   *
   */
  
   Whether c1  or C2 represent the class type of the Foo class, a class can only be an instance object of Class
  system.out.println (c1 = = C2);


Dynamic load class: Functional classes try to use dynamic load instead of static load (statically load).
Class.forName ("Full name of the class");
A. Class Type,b representing classes. Representing the dynamic load class
• Please distinguish between compiling and running
• Compile time load class is static load class (new constructor)
Runtime load class is a dynamic load class
New Create object is static load class, at compile time need to load all the possible classes (100 functions, one load is not successful, the program compiles do not pass)
This problem can be solved by dynamic loading
try{
Dynamically loading classes, loading at run time
Class c =class.forname ("ClassName1")
Create this class object with class type
ClassName1 cn = (ClassName1) c.newinstance ();
}

4) Basic Data type
The Void keyword has a class type


3-1 Java Get method information
5) Basic API operations for class
/**
* Print class information, including member functions of classes, member variables (get member functions only)
* @param obj information about the class to which the object belongs
*/
public static void Printclassmethodmessage (Object obj) {
To get information about a class, first get the class type of the class
Class C = Obj.getclass ();//The object C of which subclass is passed is the class type of the subclass
Gets the name of the class
System.out.println ("The name of the class is:" +c.getname ());
/*
* Method Class, Methods object
* A member method is an object
* The GetMethods () method gets all the public functions, including those inherited by the parent class.
* Getdeclaredmethods () gets all the methods that the class declares itself, without asking access rights
*/
Method[] ms = C.getmethods ();//c.getdeclaredmethods ()
for (int i = 0; i < ms.length;i++) {
Gets the class type of the return value type of the method
Class returntype = Ms[i].getreturntype ();
System.out.print (Returntype.getname () + "");
Get the name of the method
System.out.print (Ms[i].getname () + "(");
Gets the parameter type---> Gets the class type of the type of the parameter list
class[] Paramtypes = Ms[i].getparametertypes ();
for (Class class1:paramtypes) {
System.out.print (Class1.getname () + ",");
}
System.out.println (")");
}
}
/**
* Get information on member variables
* @param obj
*/
public static void Printfieldmessage (Object obj) {
Class C = Obj.getclass ();
/*
* Member variables are also objects
* Java.lang.reflect.Field
* Field class encapsulates operations on member variables
* The GetFields () method gets the information for all public member variables
* Getdeclaredfields Gets the information about the member variables declared by the class itself
*/
field[] fs = C.getfields ();
field[] fs = C.getdeclaredfields ();
for (Field Field:fs) {
The class type that gets the type of the member variable
Class FieldType = Field.gettype ();
String typeName = Fieldtype.getname ();
Get the name of the member variable
String fieldName = Field.getname ();
System.out.println (typename+ "" +fieldname);
}
}
/**
* Print information about the constructor of an object
* @param obj
*/
public static void Printconmessage (Object obj) {
Class C = Obj.getclass ();
/*
* Constructors are also objects
* Java.lang. Information in constructor that encapsulates the constructor
* GetConstructors get all the public constructors
* Getdeclaredconstructors get all the constructors
*/
Constructor[] cs = c.getconstructors ();
Constructor[] cs = c.getdeclaredconstructors ();
for (Constructor Constructor:cs) {
System.out.print (Constructor.getname () + "(");
Gets the argument list of the constructor---> Gets the class type of the argument list
class[] Paramtypes = Constructor.getparametertypes ();
for (Class class1:paramtypes) {
System.out.print (Class1.getname () + ",");
}
System.out.println (")");
}
}
}

Java-reflect (Reflective) 1

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.