JAVA反射-(一)快速了解反射的一些作用
本文最后更新于:April 14, 2022 pm
积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里,不积小流无以成江海。齐骥一跃,不能十步,驽马十驾,功不在舍。面对悬崖峭壁,一百年也看不出一条裂缝来,但用斧凿,能进一寸进一寸,能进一尺进一尺,不断积累,飞跃必来,突破随之。
目录
快速了解反射的一些作用。
类
| @AllArgsConstructor @NoArgsConstructor @Data @ToString class User{ private String username; private String password; private Integer age; public void setS(){ System.out.println("setS方法执行了"); } public void show(){ System.out.println("show方法执行了"); } }
|
Class获取的方式
| Class cs = Class.forName("com.annotationStu.User"); Class cs = User.class;
User u = new User(); Class cs = u.getClass();
|
获取包名
| public class AnnoTest01 { @SuppressWarnings("all") public static void main(String[] args) throws Exception { Class cs = Class.forName("com.annotationStu.User");
System.out.println(cs.getPackage().getName());
} }
|
获取类名
| public class AnnoTest01 { @SuppressWarnings("all") public static void main(String[] args) throws Exception { Class cs = Class.forName("com.annotationStu.User");
System.out.println(cs.getSimpleName());
} }
|
获取类的全限定名称
| public class AnnoTest01 { @SuppressWarnings("all") public static void main(String[] args) throws Exception { Class cs = Class.forName("com.annotationStu.User");
System.out.println(cs.getName());
} }
|
获取对象示例
| public class AnnoTest01 { @SuppressWarnings("all") public static void main(String[] args) throws Exception { Class cs = Class.forName("com.annotationStu.User");
Object o = cs.newInstance(); Object o1 = cs.getConstructor().newInstance(); System.out.println(o); System.out.println(o1);
} }
|
获取类属性名称
获取指定属性值
| public class AnnoTest01 { @SuppressWarnings("all") public static void main(String[] args) throws Exception { Class cs = Class.forName("com.annotationStu.User");
Field password = cs.getField("nu"); System.out.println(password.get(cs.getConstructor().newInstance()));
} }
|
获取类全部属性名称
| public class AnnoTest01 { @SuppressWarnings("all") public static void main(String[] args) throws Exception { Class cs = Class.forName("com.annotationStu.User"); Field[] declaredFields = cs.getDeclaredFields(); for(Field it : declaredFields){ System.out.println(it.getName()); }
} }
|
获取类中所有可用方法
| public class AnnoTest01 { @SuppressWarnings("all") public static void main(String[] args) throws Exception { Class cs = Class.forName("com.annotationStu.User"); Method[] declaredMethods = cs.getDeclaredMethods(); for(Method it: declaredMethods){ System.out.println(it.getName()); }
} }
|
通过方法名称执行方法
这里是通过反射实现,好处在于方法名称可动态设置。
| public class AnnoTest01 { @SuppressWarnings("all") public static void main(String[] args) throws Exception { Class cs = Class.forName("com.annotationStu.User"); Object o = cs.newInstance(); Method show = cs.getMethod("show"); show.invoke(o);
} }
|
给属性设值
| public class AnnoTest01 { @SuppressWarnings("all") public static void main(String[] args) throws Exception { Class cs = Class.forName("com.annotationStu.User");
Field password = cs.getField("nu"); Object o = cs.getConstructor().newInstance(); System.out.println(password.get(o)); password.set(o,"www"); System.out.println(password.get(o));
} }
|