Http://daimojingdeyu.blogbus.com/logs/5185456.html
A property ABC is defined in the class. How can I obtain the ABC name string of this variable? That is to say, how can we know that the variable ABC is called ABC.
This is an interesting problem I encountered during the opening of my hair. I found some materials for a long time and thought hard, and finally found a solution. Use reflection ~~~
Java provides a powerful reflection mechanism, which can be said to be more dynamic than dynamic languages. However, there are not many applications in this scenario.
A method is provided to dynamically obtain the variable name of an object:
Public class mytools {
Public static void setallcomponentsname (Object f ){
// Obtain all attribute fields in the class corresponding to the f object
Field [] fields = f. getclass (). getdeclaredfields ();
For (INT I = 0, Len = Fields. length; I <Len; I ++ ){
// Obtain the attribute name for each attribute
String varname = Fields [I]. getname ();
Try {
// Obtain the original access control permission
Boolean accessflag = Fields [I]. isaccessible ();
// Modify the access control permission
Fields [I]. setaccessible (true );
// Obtain the variables in the object corresponding to fields [I] in object F.
Object o = Fields [I]. Get (f );
System. Out. println ("the input object contains the following variable:" + varname + "=" + O );
// Restore the access control permission
Fields [I]. setaccessible (accessflag );
} Catch (illegalargumentexception ex ){
Ex. printstacktrace ();
} Catch (illegalaccessexception ex ){
Ex. printstacktrace ();
}
}
}
Public static void main (string [] ARGs)
{
// The test code is used to obtain all the attribute names and their attribute variables of the sound name in a jlabel.
Setallcomponentsname (New jlabel ("test "));
}
}
Fields [I]. setaccessible (true );
This statement is mainly because private variables cannot be accessed outside the object. Through this setting, the access to private variables can be changed, which is a bit scary, when I told the boss about the solution, the boss almost vomited blood and could access all the private resources. What security measures can be taken?
However, security is privileged, so it is still accepted because it can solve the problem well ~~~
Object o = Fields [I]. Get (f );
The function of this sentence is to obtain the Real Property variable corresponding to the class attribute fields [I] In the passed object F. It is a bit awkward. For example, a Class A has an attribute of int ABC, and we have two class A objects A1 and A2, so when we useFields [I]. Get (A1)
The specific ABC attribute in object A1.Fields [I]. Get (A2)
The specific ABC attribute in object A2 is obtained.
Finally, we tested a jlabel object using the main method, printed all the variable names of the sound names in the jlabel, and output their corresponding variable values. The result is as follows:
The input object contains the following variable: uiclassid = labelui
The input object contains the following variable: mnemonic = 0
The input object contains the following variable: mnemonicindex =-1
The input object contains the following variable: text = test
The input object contains the following variable: defaulticon = NULL
The input object contains the following variable: disabledicon = NULL
The input object contains the following variable: disablediconset = false
The input object contains the following variable: verticalignment = 0
The input object contains the following variable: horizontalalignment = 10
The input object contains the following variable: verticaltextposition = 0
The input object contains the following variable: horizontaltextposition = 11
The input object contains the following variable: icontextgap = 4
The input object contains the following variable: labelfor = NULL
The input object contains the following variable: labeled_by_property = labeledby
Random article: