反射练习

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
interface BaseInfo
{
public void fun();
public String returnFun();
public void oneParameterfun2(String s);
public String twoParameterfun3(String s,int a);
}
class Person implements BaseInfo
{
public static final int ABC=18;
private String name;
private int age;
public Person()
{
this.name="张三";
this.age=ABC;
//System.out.println("无参构造函数被调用");
}
public Person(String str,int i)
{
this.setName(str);
this.setAge(i);
}
public void setName(String s)
{
this.name=s;
}
public void setAge(int i)
{
this.age=i;
}
public String getName()
{
//System.out.println(this.name);
return this.name;
}
public int getAge()
{
return this.age;
}
public void fun()
{
System.out.println("这是一个无参无返回类型的函数");
}
public String returnFun()
{
return this.getName();
}
public void oneParameterfun2(String s)
{
this.setName(s);
this.setAge(ABC);
}
public String twoParameterfun3(String s,int a)
{
this.setName(s);
this.setAge(a);
return "姓名:"+this.getName()+" 年龄:"+this.getAge();
}
public String toString()
{
return "基本信息:\n\t|-姓名:"+this.getName()+" 年龄:"+this.getAge();
}

}
public class ReflectTest
{
public static void main(String[] args) throws Exception
{
// Person per=new Person();
//三种获取Class对象的方法
Class<?> c1=Class.forName("Person");
// Class c2=per.getClass();
// Class c3=Person.class;
// Person per1=(Person)c1.newInstance();//newInstance会调用默认的无参构造函数
// System.out.println(per1);
//调用指定的构造函数
// Constructor<?> cs=c1.getConstructor(String.class,int.class);
// Person per2=(Person)cs.newInstance("李四",22);
// System.out.println(per2);

//用反射的方式拼凑所有的构造函数
// Constructor<?>[] cs=c1.getConstructors();
// for(int i=0;i<cs.length;i++)
// {
////System.out.println(cs[i]);
// System.out.print(Modifier.toString(cs[i].getModifiers())+" ");
// System.out.print(cs[i].getName()+"(");
// Class<?>[] per=cs[i].getParameterTypes();
// for(int j=0;j<per.length;j++)
// {
// System.out.print(per[j].getName()+" arg-"+j);
// if(j<per.length-1)
// System.out.print(",");
// }
// System.out.println(")");
// }

//调用指定的成员方法--有两个参数,有返回类型的函数
// Method md=c1.getMethod("twoParameterfun3",String.class,int.class);
// String str=(String)md.invoke(c1.newInstance(),"王麻子",23);
// System.out.println(str);
//调用指定的成员方法--无参数,无返回类型
// Method md1=c1.getMethod("fun");
// md1.invoke(c1.newInstance());

//用反射的方式拼凑输出所有的成员方法
// Method[] md=c1.getMethods();
// for(int i=0;i<md.length;i++)
// {
////System.out.println(md[i]);
// System.out.print(Modifier.toString(md[i].getModifiers())+" ");
// System.out.print(md[i].getReturnType().getName()+" ");
// System.out.print(md[i].getName()+"(");
// Class<?>[] per=md[i].getParameterTypes();
// Class<?>[] exp=md[i].getExceptionTypes();
// for(int j=0;j<per.length;j++)
// {
// System.out.print(per[j].getName()+" args-"+j);
// if(j<per.length-1)
// System.out.print(",");
// }
// if(exp.length!=0)
// {
// System.out.println(")"+" throws");
// for(int k=0;k<exp.length;k++)
// {
// System.out.println("\t"+exp[k].getName());
// }
// }
// else
// {
// System.out.println(")");
// }
//
// }

//用反射的方式实现setter和getter
// Object oj=c1.newInstance();
// setter(oj,"name","龙五",String.class);
// setter(oj,"age",26,int.class);
// getter(oj,"name");
// getter(oj,"age");

//用反射的方式访问属性
//Field fe=c1.getDeclaredField("name");
Field[] fe=c1.getDeclaredFields();
for(int i=0;i<fe.length;i++)
{
fe[i].setAccessible(true);
Object oj=fe[i].get(c1.newInstance());
System.out.println(oj);
}



}
public static void setter(Object o,String s,Object i,Class<?> type) throws Exception
{
Method md=o.getClass().getMethod("set"+firstLetter(s),type);
md.invoke(o,i);
}
public static void getter(Object o,String s) throws Exception
{
Method md=o.getClass().getMethod("get"+firstLetter(s)) ;
Object oj=md.invoke(o);
System.out.println(oj);
}
public static String firstLetter(String s)
{
StringBuffer sBf=new StringBuffer();
sBf.append(s.substring(0,1).toUpperCase()).append(s.substring(1));
return sBf.toString();
}
}
原文地址:https://www.cnblogs.com/xiongyu/p/2237269.html