反射----

反射
1.获取Class对象的方法
public class Test {
	public static void main(String[] args) {
		//通过类获取Class对象
		Class clazz1 = Student.class;
		//通过对象获取Class对象
		Student s = new Student(18, 0);
		Class clazz2 = s.getClass();
		//通过forName静态方法
		try {
			Class clazz3 = Class.forName("cn.itlaobing.ref.Student");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
2.获取构造器
方法描述
Connstructor getConstructor(Class<?>…paramterTypes)获取Class对象对应类的带指定参数列表的public构造器,如果参数列表中参数的class对象和构造方中参数列表类型不匹配或者构造方法不是public修饰的,则抛出NoSuchMethodException
Connstructor<?> getContructors()获取Class对象对应类的所有public构造器
Connstructor getDeclaredConstructor (Class<?>…paramterTypes)获取Class对象对应类的带指定参数列表的构造器,与访问修饰符无关
Connstructor<?> getDeclaredConstructors()获取Class对象对应类的所有构造器,与访问修饰符无关
2.1示例
public class Test2 {
	
	public static void main(String[] args) throws NoSuchMethodException, SecurityException {
		
		Class<Student> clazz = Student.class;
		
		//获取public修饰的构造方法
		Constructor[] c1 = clazz.getConstructors();
		
//		for (Constructor constructor : c1) {
//			System.out.println(constructor);
//		}
		//获取所有构造方法,与访问修饰符无关
		Constructor[] c2 = clazz.getDeclaredConstructors();
//		for (Constructor constructor : c2) {
//			System.out.println(constructor);
//		}
		//获取指定的public修饰的构造方法
		Constructor c = clazz.getConstructor(int.class,double.class);
		System.out.println(c);
		
//		Constructor tempc = clazz.getConstructor(double.class);
		
		//获取指定参数的构造器,与访问修饰符无关
		Constructor construct1 = clazz.getDeclaredConstructor(double.class);
		System.out.println(construct1);
		
		Constructor construct2 = clazz.getDeclaredConstructor();
		
		System.out.println(construct2);
 	}
	
}
3.获取成员方法
方法描述
Method getMethod(String name,Class<?>…paramterTypes)获取指定名称和参数类型class对象的public修饰方法,包括从父继承的public修饰的方法
Method[] getMethods()获取所有public修饰的成员方法以及继承的方法,包括类方法
Method getDeclaredMethod(String name,Class<?>…paramterTypes)获取指定名称和参数类型class对象的方法,与访问修饰符无关
Method[] getDeclaredMethods()获取自身声明的所有成员方法,与访问修饰符无关
3.1示例
public class MethodTest {
	public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		//获取Class对象
		Student s = new Student(0);
		Class clazz = s.getClass();
		//获取public修饰的方法包含父类public修饰的方法
		Method[] methods = clazz.getMethods();
		
//		for (Method method : methods) {
//			System.out.println(method);
//		}
		//获取自身声明的所有方法,与访问修饰符无关
		Method[] methods2 = clazz.getDeclaredMethods();
		
//		for (Method method : methods2) {
//			System.out.println(method);
//		}
		//获取指定参数的public修饰的成员方法
//		Method method = clazz.getMethod("newObject");
//		System.out.println(method);
		//获取指定参数的自身声明的方法,与访问修饰符无关
		Method method2 = clazz.getDeclaredMethod("newObject", int.class);
//		System.out.println(method2);
//		method2.setAccessible(true);
//		method2.invoke(s,2);
		Method method3 = clazz.getDeclaredMethod("makeMoney");
		method3.invoke(s);
		Method method4 = clazz.getDeclaredMethod("sleep",String.class);
		method4.invoke(null,"蔡老师");
	}
}
4.获取成员变量
方法描述
Field getField(String name)获取指定名称的public修饰的成员变量
Field[] getFields()获取所有的public修饰的成员变量,包括从父类继承的成员变量
Field getDeclaredField(String name)获取指定名称的成员变量,与访问权限无关
Field[] getDeclaredFields()获取所有的成员变量,与访问修饰符无关
4.1示例
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		Son s = new Son();
		Class clazz = s.getClass();
		//获取自身和父类public修饰的成员变量
		Field[] fields = clazz.getFields();
//		for (Field field : fields) {
//			System.out.println(field);
//		}
		//获取自身声明的成员变量,与访问修饰符无关
		Field[] fields2 = clazz.getDeclaredFields();
//		for (Field field : fields2) {
//			System.out.println(field);
//		}
		//根据指定名称获取自身和父类public修饰的成员变量属性
		Field f1 = clazz.getField("age");
		f1.setInt(s, 10);
		System.out.println(s.age);
		
		Field f3 = clazz.getField("name");
		f3.set(s, "张三");
		System.out.println(s.name);
		//根据指定名称获取属性,与访问修饰符无关
		Field f2 = clazz.getDeclaredField("width");
		System.out.println(f2);
		f2.setAccessible(true);
		f2.setInt(s, 100);
		System.out.println(s.getWidth());
		
	}
5.获取注解
6.Method对象方法
方法描述
invoke(Object obj,Object…obj)调用对象方法
setAccessible(boolean flag)设置私有方法可访问
7.实例化对象
方法描述
clazz.newInstance()只能调用默认无参构造方法
Constructor.newInstance(Object…obj)调用对应的构造方法
7.1示例
public class NewInstance {
	
	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		
		Class clazz = Class.forName("cn.itlaobing.ref2.Student");
		Constructor c = clazz.getDeclaredConstructor(int.class);
		c.setAccessible(true);
		//利用构造方法newInstanc方法
		Student s = (Student)c.newInstance(18);
		
		System.out.println(s.getAge());
		//利用class对象newInstance,调用默认无参构造
//		Student s2 = (Student) clazz.newInstance();
//		System.out.println(s2.getAge());
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值