java的反射学习总结

java的反射

1.what is 反射?

​ 不通过源码本身就能获得类的内部构造,获得对象,可以动态的调用方法的技术

2.能做什么?

  • 创建对象

  • 获得类的成员

  • 调用方法

  • 访问属性

  • 消除耦合性

    	private static void test1() 
    			throws ClassNotFoundException,InstantiationException,IllegalAccessException{
    		String className = "com.wty.d0907.User";//全限定类名
    		Class klazz = Class.forName(className);
    		User user = (User) klazz.newInstance();
    		System.out.println(user);//这是个真正的对象
    	}
    }
    
    	private static void test2() {
    		BMW bmw = new BMW();//死板固定
    		Car car = new BYD();//灵活,耦合性低(解耦,消耦)
    //		LinkedList list = new LinkedList<>();//耦合性大,只能使用LinkedList,违反依赖倒置原则,原则 依赖是面向接口的,而不是具体的实现类
    		//简单(对象)工厂模式
    		List list = new LinkedList<>();
    		car.run();
    	}
    
    	private static void test3() throws FileNotFoundException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    		Properties properties = new Properties();//继承自Hashtable是一个map集合
    		properties.load(new FileReader("src/com/wty/d0907/car.properties"));
    		System.out.println(properties);
    		Class klazz = Class.forName(properties.getProperty("cartype"));
    		Car car = (Car)klazz.newInstance();
    		car.run();
    	}
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Yo4x83cY-1663331446289)(9.7总结.assets/QQ截图20220907210019.png)]

3.怎么反射?

​ 3.1 如何获得Class的对象

  • 类的static属性 class

  • 对象的getClass()方法

  • 包装类的TYPE属性,返回的是基本数据类型Class

  • Class.forName(“类名”)

    	private static void test4_getClass() {
    		Class class1 = User.class;//类得到
    		Class class2 = new User().getClass();//对象得到的
    		Class class3 = Integer.class;
    		Class class4 = Integer.TYPE;
    	}
    

    3.2反射Class的成员

	private static void test5_getConstructor() throws ClassNotFoundException, NoSuchMethodException, SecurityException {
		String className = "com.wty.d0907.User";
		Class klazz = Class.forName(className);
		Constructor[] constructors = klazz.getConstructors();//得到构造器的数组
//		Constructor[] constructors = klazz.getDeclaredConstructors();
		System.out.println(Arrays.toString(constructors));
//		Constructor constructor = klazz.getConstructor();//无参
		Constructor constructor = klazz.getConstructor();
		System.out.println(constructor);
	}
	private static void test6_newInstance() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		String className = "com.wty.d0907.User";
		Class klazz = Class.forName(className);
//		User user = (User) klazz.newInstance();
		Constructor constructor = klazz.getDeclaredConstructor(int.class);
		constructor.setAccessible(true);
		User user = (User) constructor.newInstance(1234);
		System.out.println(user);
	}
	private static void test7_getField() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException{
		String className = "com.wty.d0907.User";
		Class klazz = Class.forName(className);
		Field[] declaredFields = klazz.getDeclaredFields();
		System.out.println(Arrays.toString(declaredFields));
		Field deckaredField = klazz.getDeclaredField("name");
		User user = (User) klazz.newInstance();
		deckaredField.setAccessible(true);
		deckaredField.set(user, "王阳明");
		System.out.println(user);
	}
	private static void test9_getparent() throws Exception{
		String className = "com.wty.d0907.User";
		Class klazz = Class.forName(className);
		System.out.println(klazz.getName());//com.wty.d0907.User
		System.out.println(klazz.getSimpleName());//User
		System.out.println(klazz.getSuperclass());//class java.lang.Object
		System.out.println(klazz.getInterfaces());//[interface java.io.Serializable, interface java.lang.Cloneable]
		System.out.println(Arrays.toString(klazz.getInterfaces()));
		System.out.println(Arrays.toString(klazz.getDeclaredClasses()));//[class com.wty.d0907.User$InnerClass]
	}
	
	private static void test8_getMethod() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException{
		String className = "com.wty.d0907.User";
		Class klazz = Class.forName(className);
//		Method[] methods = klazz.getMethods();
//		System.out.println(Arrays.toString(methods));
		Method method = klazz.getMethod("setName",String.class);
//		Method method = klazz.getMethod("notify");
//		System.out.println(method.getDeclaringClass());
		User user = (User) klazz.newInstance();
		method.invoke(user, "朱熹");
		System.out.println(user);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值