Ok before said the reflection and reflection analysis of Java class, then these things have a horse effect, the following applications:
Iii. using reflection to analyze objects at run time
- Simply write aEmployeeClass, then use java reflex to take name domain Getdeclarefield (string name) can get all , even private above said, so to the 9< Span style= "FONT-FAMILY:SIMSUN; font-size:11pt, "lang=" ZH-CN "> OK.
,
But in the first11 Yes, it was wrong , because get (Field f) No access so illegalaccessexception, Here's a way Setaccessible (Boolean b), This cargo is for Debug, persistent storage, and similar mechanisms provide finish reading the following :
Access, of course, is easy to use Field.set (E, "hehe"); Such statements are assigned to a value ~
Iv. Reflection -- generic array
Array (java.lang.reflect) allows you to create arrays dynamically, following the implementation code:
But this code is object[]object[]string[],string[] turn into object[]
Error:classcastexception:
So how is the conversion correct? , array There is a method called newinstance (class ) Span style= "FONT-FAMILY:CALIBRI;" lang= "en-US" ><?> componenttype, int length); This method you just given the array of the original type and the length of the array, then return come out objectstring[].
Maincode:
Method: (Arrayprint (int[]) is the print function , Goodarraygrow (int[] a) is the growth array function )
Result:
Succeeded in increasing the array hohoho~
Java Reflection Learning (Java reflect) (ii)