Some common methods reflection

1.1 acquire Class instances (three kinds):

- Class c = class name .class
   - Class c = the Class.forName ( "fully qualified class name of the class");
    - Class c = Object .getClass ();
. 1
2
. 3
1.2, acquires the object class name

 - String className = c.getName (); // Get the fully qualified class name
    - String className = c.getSimpleName (); // Get simple names
. 1
2
1.3 acquiring Field (four ways):

- Field field = c.getField ( "attribute name"); // This method can only be obtained by the public Attribute Name Attribute
    - Field [] field = c.getFields ( ); // get all the public properties of the array
    - Field field = c.getDeclaredField ( "attribute name"); // get property class, including protected / Private
. 1
2
. 3
1.4, obtaining information Field

- String name = field.getName (); // get the property name
    - Class type = field.getType (http://www.amjmh.com/v/) <?>; // get the type of property
    - Object value = // Get the value of the field attribute object obj; Field.get (obj)
    - field.set (obj, object value); // attribute field to the object obj assignment value
. 1
2
. 3
. 4
for 1.5, provided accessibility

- setAccessible (true); // can be used where access modifier modified,
// default is false only modified the operation of public, private setting true to the operation of the modified
---------- -----------

Guess you like

Origin www.cnblogs.com/hyhy904/p/11335124.html