Java 反射

来源:互联网 发布:淘宝立体服装拍摄 编辑:程序博客网 时间:2024/05/19 07:07

demo1


import java.lang.reflect.Method;


public class DumpMethods
{
public static void main(String[] args) throws Exception
{
Class<?> classType  = Class.forName("java.lang.String");


Method[] methods = classType.getDeclaredMethods();

for(Method method : methods)
{
System.out.println(method);
}

}
}


demo2:

import java.lang.reflect.Method;


public class InvokeTester
{
public int add(int a ,int b )
{
return a + b;
}

public String echo(String name)
{
return  "hello:" + name;
}


public static void main(String[] args) throws Exception
{
Class<?> classType = InvokeTester.class; //获得一个确定类对应的一个class类的一种方式

Object invokeTester = classType.newInstance();

System.out.println(invokeTester instanceof InvokeTester); //判断是否是InvokeTester的对象

Method addMethod = classType.getMethod("add",new Class[]{int.class,int.class});   //通过指定方法的名字和用 new Class[]{}数组的方式指定所要动态调用的方法

Object result  = addMethod.invoke(invokeTester,new Object[]{1,2}); //声明在哪个对象上调用方法,已经通过Object数组的方式给方法传递参数



System.out.println((Integer)result);

System.out.println("------------------");

Method echoMethod = classType.getMethod("echo",new Class[]{String.class});

System.out.println((String)echoMethod.invoke(invokeTester,new Object[]{"tom"}));

}
}




反射要开始首先第一步 :一个类对应的Class对象。

获取某个类或某一个对象所对应的Class对象 的常用3中方法

a)使用Class类的静态方法 forName:Class.forName("java.lang.String");

b)使用类的.class语法:String.class

c)使用对象的getClass()方法 : String s = "aa";  Class<?> clazz = s.getClass();


反射中生成对象的两种方法  

a)先获得Class对象,然后 通过该Class对象的newInstance()方法直接生成即可:

Class<?> classType = String.class;

Object obj = classType.newInstance();

b)先获得Class对象,然后通过该对象获得对应的Constructor对象,

再通过该Constructor 对象 的newInstance()方法生成:

Class<?> classType = Customer.class;

Constructor cons = classType.getConstructor(new Class[]{});  //这里找构造方法不用想找普通的方法那样指定方法的名字,因为构造方法名与类名一样!

Object obj = cons.newInstance(new Obejct[]{});


但是如果要生成带参数的构造方法 只能选择b)方式去生成。








通过反射动态地将一个对象里的所有的属性拷贝到一个新的对象当中  代码如下所示:

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;


public class ReflectTester
{
public Object copy(Object object) throws Exception
{
Class<?> classType = object.getClass();

Constructor cons = classType.getConstructor(new Class[]{});

Object objectCopy = cons.newInstance(new Object[]{});

Field[] fields = classType.getDeclaredFields();

for(Field field : fields)
{
String name = field.getName();

String firstLetter = name.substring(0,1).toUpperCase();

String getMethodName = "get" + firstLetter + name.substring(1);

String setMethodName = "set" + firstLetter + name.substring(1);

Method getMethod = classType.getMethod(getMethodName,new Class[]{});

Method setMethod = classType.getMethod(setMethodName,new Class[]{field.getType()});

Object value = getMethod.invoke(object,new Object[]{});


setMethod.invoke(objectCopy,new Object[]{value});
}

return objectCopy;
}

public static void main(String[] args) throws Exception
{
ReflectTester test = new ReflectTester();

Customer customer = new Customer("tom",20);

customer.setId(1L);



Customer result = (Customer)test.copy(customer);

System.out.println(result.getAge());
System.out.println(result.getName());
System.out.println(result.getId());

}
}


class Customer
{
private Long id ;
private String name ;
private int age;

public Customer()
{

}

public Customer(String name ,int age)
{
this.name = name;
this.age = age;
}


public Long getId()
{
return id;
}


public void setId(Long id)
{
this.id = id;
}


public String getName()
{
return name;
}


public void setName(String name)
{
this.name = name;
}


public int getAge()
{
return age;
}


public void setAge(int age)
{
this.age = age;
}


}


0 0
原创粉丝点击