反射 for 本方法
输出结果:
public void com.fanshe.Relf.aaaa()
public static void com.fanshe.Relf.main(java.lang.String[]) throws java.lang.Exception
反射 for 属性 改变一个类中的一个属性
反射 for 方法 不需要调用方法,输入方法名即可
http://zhidao.baidu.com/question/151090808.html
package com.fanshe;
import java.lang.reflect.Method;
public class Relf {
public void aaaa(){
}
public static void main(String args[]) throws Exception {
Class c =Class.forName("com.fanshe.Relf");
Method m[] = c.getDeclaredMethods();
for(int i=0;i<m.length;i++){
System.out.println(m[i]);
}
}
}
输出结果:
public void com.fanshe.Relf.aaaa()
public static void com.fanshe.Relf.main(java.lang.String[]) throws java.lang.Exception
反射 for 属性 改变一个类中的一个属性
package com.fanshe;
import java.lang.reflect.Field;
public class ReflforField {
public int d;
public static void main(String[] args) throws Exception {
Class c = Class.forName("com.fanshe.ReflforField");
Field fd =c.getField("d");
ReflforField fobj =new ReflforField();
System.out.println(fobj.d);
fd.setInt(fobj, 15);
System.out.println(fobj.d);
}
}
反射 for 方法 不需要调用方法,输入方法名即可
package com.fanshe;
import java.lang.reflect.Method;
public class RelfforMethodname {
public int add(int a, int b)
{
return a + b;
}
public static void main(String[] args) throws Exception {
Class cl = Class.forName("com.fanshe.RelfforMethodname");
Class partypes[] = new Class[2];
partypes[0]=Integer.TYPE;
partypes[1]=Integer.TYPE;
Method m =cl.getMethod("add",partypes);
RelfforMethodname rfm = new RelfforMethodname();
Object al[] =new Object[2];
al[0] =new Integer(37);
al[1] =new Integer(370);
Object retobj = m.invoke(rfm, al);
Integer rv =(Integer)retobj;
System.err.println(rv.intValue());
}
http://zhidao.baidu.com/question/151090808.html