Java Basic Note 14

Source: Internet
Author: User

1. Reflections.
Class: Reflection class Any one class has a class reflector. Shadow
java.lang.reflect.*;
Field: Fields Class
Method: Methods Class class
Constructor: Constructs a method class.

2. Get the Reflection class
1. Get the Reflection class class name. class
Class Clazz1=people.class;
2. Through the full class name
Class Clazz2=class.forname ("demo. People ");
3. Object acquisition
People p=new people ();
Class Clazz3=p.getclass ();
3. Instantiate the corresponding class object by reflection class.
Clazz.newinstance ();

4. Gets the properties in the class through the reflection class.
Gets the properties of the parent class or the public adornment in this class
Field agefield= Clazz1.getfield ("Age");
Agefield.set (P, 156);
Get only the properties that you declare in this class
Field Namefield=clazz1.getdeclaredfield ("name");
Namefield.setaccessible (TRUE);//Allow assignment
Namefield.set (P, "Zhang San");
SYSTEM.OUT.PRINTLN (P);
5. Get all the methods of a class
Method showmethod= Clazz1.getmethod ("print", int.class);
Call this method
Showmethod.invoke (p,15);

Method[] Ms=clazz1.getdeclaredmethods ();
for (Method m:ms) {
System.out.println (Modifier.tostring (M.getmodifiers ()) + ""
+m.getreturntype (). GetName () + "" +m.getname () + "(" +arrays.aslist (M.getgenericparametertypes ()) + ")");
}











Java Basic Note 14

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.