实例:
public class User {
private int id;
private int age;
private String name;
public int getId() {
return id;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public User(){
}
public User(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
}
public class ReflectTest01 {
public static void main(String[] args) {
String path="com.ztc.reflect.User";
try {
//获取类的实例
Class clazz=Class.forName(path);
//获取类的名字
System.out.println(clazz.getName());
System.out.println(clazz.getSimpleName());
//获取属性信息
//Field[] fields=clazz.getFields();//只能获取public的属性信息
Field[] fields=clazz.getDeclaredFields();//获取所有的属性
Field f=clazz.getDeclaredField("id");//获取特定名字的属性
for (Field field : fields) {
System.out.println("属性:"+field);
}
System.out.println(fields.length);
System.out.println(f);
//获取方法信息
Method[] methods=clazz.getDeclaredMethods();
Method m01=clazz.getDeclaredMethod("getName",null);
Method m02=clazz.getDeclaredMethod("setName",String.class);
for (Method method : methods) {
System.out.println("方法:"+method);
System.out.println(m01);
System.out.println(m02);
}
//获得构造器信息
Constructor[] constructors=clazz.getDeclaredConstructors();
for (Constructor constructor : constructors) {
System.out.println("构造器:"+constructor);
}
Constructor c=clazz.getDeclaredConstructor(int.class,int.class,String.class);
System.out.println(c);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.ztc.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by ZHANGTIANCHENG on 2017/1/9.
*/
public class ReflectTest02 {
public static void main(String[] args) {
String path="com.ztc.reflect.User";
try {
Class<User> clazz= (Class<User>) Class.forName(path);
//动态调用构造方法
User u=clazz.newInstance();//调用javabean无参的构造方法
System.out.println(u);
Constructor<User> c=clazz.getDeclaredConstructor(int.class,int.class,String.class);
User u2=c.newInstance(110,18,"ztc");
System.out.println(u2.getName());
//动态调用普通方法
User u3=clazz.newInstance();
Method m01=clazz.getDeclaredMethod("setName",String.class);//好处:方法是变量,实现动态调用
m01.invoke(u3,"张天诚");
System.out.println(u3.getName());
//操作属性
Field f=clazz.getDeclaredField("name");
User u4=clazz.newInstance();
f.setAccessible(true);//设置可以直接访问私有属性
f.set(u4,"zhuya");//通过反射写属性
System.out.println(f.get(u4));//通过反射读属性
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用反射会使程序变慢(大约30倍)
提高反射的执行效率:setAccessible(true) 禁用安全检查(可访问私有),提高效率,大约4倍
反射操作泛型
Java采用泛型擦除机制引入泛型,确保数据的安全性和免去强制类型转换的麻烦,一旦编译完成,擦除泛型
public class ReflectTest03 {
public void test01(Map<String,User> map, List<User> list){
System.out.println("test01");
}
public Map<Integer,User> test02(){
System.out.println("test02");
return null;
}
public static void main(String[] args) {
try {
Method m=ReflectTest03.class.getDeclaredMethod("test01",Map.class,List.class);
Type[] types=m.getGenericParameterTypes();//获得带泛型的参数类型
for (Type type : types) {
System.out.println("#"+type);
if(type instanceof ParameterizedType){
Type[] genericTypes=((ParameterizedType) type).getActualTypeArguments();//获得泛型类型
for (Type genericType : genericTypes) {
System.out.println("泛型类型:"+genericType);
}
}
}
Method m2=ReflectTest03.class.getDeclaredMethod("test02",null);
Type returnType=m2.getGenericReturnType();
System.out.println(returnType);//java.util.Map<java.lang.Integer, com.ztc.reflect.User>
if(returnType instanceof ParameterizedType){
Type[] genericTypes=((ParameterizedType) returnType).getActualTypeArguments();//class java.lang.Integer,class com.ztc.reflect.User
for (Type genericType : genericTypes) {
System.out.println("返回值,泛型类型:"+genericType);
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}```