锘??xml version="1.0" encoding="utf-8" standalone="yes"?> However, suppose you want to restrict the output to all fields other than those declared as private. In this case, you would use the following code, where the Modifier class is a part of the java.lang.reflect package: The same can be used for methods as well.
where c is initialized using
Field fields[] = c.getDeclaredFields( );
and print the fields array, you get all the elements declared in the class.
Class c = Class.forName(className);
Using Modifier.isPrivate() ensures that the private variables are not printed.
if(!Modifier.isPrivate(fields[i].getModifiers( )){
System.out.println(fields[i]+"\n");
}
]]>
first ,useing Class.getFields to get all the fields,the use filed.get(null) to get the field's value that equals the int type will be returned.
so ,field.get(null) can return the field's value.if the parameter nonequals null,what it will return?
in addition,when use field.get(null),it use Modifier.isStatic( field.getModifiers() ) to check weather the filed is static.why it do this?if the field isn't static ,it will be what?
in my next test,
only public field can recieved by Class.getFields,even the page-privilege can't.
and only the static field can be return it's value when use filed.get(null),else will be error.
import java.lang.reflect.*;
import java.util.*;
public class DynTest {
public double d; // a field
public int add(int a, int b)
{
System.out.println("add() invoked");
return a + b;
}
public String tName(String s, Hashtable ht)
{
System.out.println("tName() invoked");
return s;
}
public static void main(String args[])
{
//浠e垰1. Method.invoke()
{
try {
Class c = Class.forName("DynTest");
Class pTypes[] = new Class[2];
pTypes[0] = Integer.TYPE;
pTypes[1] = Integer.TYPE;
Method m = c.getMethod("add", pTypes);
DynTest obj = new DynTest();
Object arg[] = new Object[2];
arg[0] = new Integer(37);
arg[1] = new Integer(47);
Object r = m.invoke(obj, arg);
Integer rVal = (Integer)r;
System.out.println("return: "+rVal.intValue()); //84
}
catch (Throwable e) {
System.err.println(e);
}
}
//浠e垰2. Method.invoke()
{
try {
Class c = Class.forName("DynTest");
Class pTypes[] = new Class[2];
pTypes[0] = Class.forName("java.lang.String");
pTypes[1] = Class.forName("java.util.Hashtable");
Method m = c.getMethod("tName", pTypes);
Object arg[] = new Object[2];
arg[0] = new String("return: "+"Hello,World!");
arg[1] = null;
//ERROR: non-static variable this cannot be referenced from a static context
DynTest obj = new DynTest();
Object r = m.invoke(obj, arg);
String rVal = (String)r;
System.out.println(rVal); //Hello,World!
}
catch (Throwable e) {
System.err.println(e);
}
}
//浠e垰3. Field.setDouble()
{
try {
Class c = Class.forName("DynTest");
Field f = c.getField("d");
DynTest obj = new DynTest();
System.out.println("before setting, d= " + obj.d); //0.0
f.setDouble(obj, 12.34);
System.out.println("after setting, d= " + obj.d); //12.34
}
catch (Throwable e) {
System.err.println(e);
}
}
} //main
}
Class object 璇炵敓綆¢亾 |
紺轟緥 |
榪愮敤getClass() 娉細姣忎釜class 閮芥湁姝ゅ嚱鏁?/SPAN> |
String str = "abc"; Class c1 = str.getClass(); |
榪愮敤 Class.getSuperclass()2 |
Button b = new Button(); Class c1 = b.getClass(); Class c2 = c1.getSuperclass(); |
榪愮敤static method Class.forName() 錛堟渶甯歌浣跨敤錛?/SPAN> |
Class c1 = Class.forName ("java.lang.String"); Class c2 = Class.forName ("java.awt.Button"); Class c3 = Class.forName ("java.util.LinkedList$Entry"); Class c4 = Class.forName ("I"); Class c5 = Class.forName ("[I"); |
榪愮敤 .class 璇硶 |
Class c1 = String.class; Class c2 = java.awt.Button.class; Class c3 = Main.InnerClass.class; Class c4 = int.class; Class c5 = int[].class; |
榪愮敤 primitive wrapper classes 鐨?/SPAN>TYPE 璇硶 |
Class c1 = Boolean.TYPE; Class c2 = Byte.TYPE; Class c3 = Character.TYPE; Class c4 = Short.TYPE; Class c5 = Integer.TYPE; Class c6 = Long.TYPE; Class c7 = Float.TYPE; Class c8 = Double.TYPE; Class c9 = Void.TYPE; |