目录
1.1、Object类中的getClass()方法:public final Class getClass()
1.2、利用“包.类.class”的形式实例化class类对象
一、概述
java的反射机制在很多地方都有用到,特别是各种框架中;通过反射,可以获取一个类的属性,属性的修饰符,属性定义的类型;构造方法,构造方法的参数,构造方法参数的类型,类中定义的各种方法,方法的修饰符,方法里的参数以及类型;类类型,类上的注解信息等。可以说使用了反射机制,一个类就变成了“没穿衣服”的家伙,什么都能被看得清清楚楚。
java的反射用到了一下两处的类:
java.lang.Class<T>
java.lang.reflect包下的类:
java.lang.reflect.Field 与属性有关的类
java.lang.reflect.Method 与方法有关的类
java.lang.reflect.Constructor 与构造方法有关的类
二、Class
1、获取一个类的类类型有三种方法:
1.1、Object类中的getClass()方法:public final Class<?> getClass()
String s = "ddd";
Class<?> cls = s.getClass();
System.out.println(cls);
1.2、利用“包.类.class”的形式实例化class类对象
Class<?> clss = java.lang.String.class;//通过对象获取对象的类类型
System.out.println(clss);
1.3、利用Class类提供的forName()方法
Class<?> cls1 = null;
try {
cls1 = Class.forName("java.lang.String");//通过对象forName获取
System.out.println(cls1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
第三种方法是最常用的方法。
Class是所有反射中最重要的类,也是反射操作的源头,而对于每个结构(构造、方法、成员或属性)都可以利用Class找到;下面将利用类的反射
1.4、通过类类型获取对象
Class<?> cls = Class.forName("study.reflaction.Person");
Person obj = (Person) cls.newInstance();
cls.newInstance();
注意:在用Class的 newInstance()方法时,类(Person)必须有无参构造方法。
当构造方法只有带参数的构造方法时,在实例化对象的时候,newInstance()方法里也要加上参数
1.5、通过类类型获取类的构造方法
取得该类public所有构造方法: public Constructor<?>[] getConstructors()throws SecurityException
取得该类public且指定类型构造方法:public Constructor<T> getConstructor(Class<?>... parameterTypes)throws NoSuchMethodException,SecurityException
取得该类所有构造方法: public Constructor<?>[] getDeclaredConstructors()throws SecurityException
取得该类且指定类型构造方法:public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)throws NoSuchMethodException,SecurityException
在反射的过程中,是一个只认类型不认具体的工具类,包括在进行方法重载的时候也是只认方法名称和参数类型。以上的两个方法返回的是
java.lang.reflact.Constrator<T>类对象,这个类常用有如下的方法
获取构造方法的名称: public String getName()
获取构造方法的修饰符:public int getModifiers()
这里方法返回的是数字,要想把它转化为具体的修饰符,可通过java.lang.reflact.Modifier的toString()方法实现。
获取构造方法的参数类型: public Class<?>[] getParameterTypes()
获取构造方法上所有的异常:public Class<?>[] getExceptionTypes()
实例:
package org.mustang.spring.java.lang.reflact;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
/**
* @author:zhangfd
* @version:1.0
* @date:2018年7月19日 下午10:40:47
* @description:
*/
public class ConstructorTest {
public static void main(String[] args) throws Exception {
//指定类的路径
String classPath = "org.mustang.spring.java.lang.reflact.Person";
//获取Person类的类类型
Class<?> obj = Class.forName(classPath);
//返回该类的所有构造方法
Constructor<?>[] constructorArray01 = obj.getDeclaredConstructors();
/*for (Constructor<?> constructor : constructorArray01) {
System.out.println(constructor.getName()+","+constructor.getParameterCount());
}*/
//仅仅返回该类声明为public的构造方法
/*Constructor<?>[] constructorArray02 = obj.getConstructors();
for (Constructor<?> constructor : constructorArray02) {
System.out.println(constructor.getName()+","+constructor.getParameterCount());
}*/
//Constructor<?> class01 = obj.getConstructor(String.class);
Constructor<?> class02 = obj.getConstructor(String.class,Integer.class);
System.out.println(Modifier.toString(class02.getModifiers())+" "+class02.getName()+"("+class02.getParameterTypes()[0].getSimpleName() +" arg0,"+class02.getParameterTypes()[1].getSimpleName()+" arg1)");
//由于构造方法里有int型的基本数据类型,这里使用反射获取不到值,所以构造方法阿狸不要用int型!!!
//Constructor<?> class03 = obj.getConstructor(String.class,Integer.class,Integer.class,BigDecimal.class);
Constructor<?> class04 = obj.getDeclaredConstructor(String.class);
System.out.println(Modifier.toString(class04.getModifiers())+" "+class04.getName()+"("+class04.getParameterTypes()[0].getSimpleName() +" arg0"+" )");
}
}
class Human{
private String country;
public Human() {
super();
}
public Human(String country) {
super();
this.country = country;
}
}
class Person extends Human{
Person(){
super();
}
protected Person(String name){
super();
this.name = name;
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Person(Integer age, int high, BigDecimal hight) {
super();
this.age = age;
this.high = high;
this.hight = hight;
}
public Person(String name, Integer age, int high, BigDecimal hight) {
super();
this.name = name;
this.age = age;
this.high = high;
this.hight = hight;
}
//姓名
private String name;
//身高
private Integer age;
//身高
private int high;
//体重
private BigDecimal hight;
}
1.6 通过类类型获取类的方法
Class类里提供了4个方法获取类的方法信息:
返回该类或接口(注意不是父类)的所有方法. public Method getDeclaredMethod(String name, Class<?>... parameterTypes)throws NoSuchMethodException,SecurityException
返回该类或接口(注意不是父类)的所以声明为public类型的方法. public Method[] getDeclaredMethods() throws SecurityException
通过方法名和参数类型获取该方法,前提是该方法必须是public. public Method[] getMethods() throws SecurityException
通过方法名和参数类型获取该方法. public Method getMethod(String name,Class<?>... parameterTypes)throws NoSuchMethodException, SecurityException
1.7 通过类类型获取类的属性
在Clss类里提供了两组获取Field的方法:
取得全部成员 public Field[] getFields()throws SecurityException
取得单个成员 public Field getField(String name) throws NoSuchFieldException, SecurityException
取得全部成员 public Field[] getDeclaredFields() throws SecurityException
取得单个成员 public Field getDeclaredField(String name)throws NoSuchFieldException, SecurityException
1.8 通过类类型获取类的属性