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);
}