Java 反射机制
作者:LL林1997
这篇文章简要的说明了Java的反射机制,Java的反射是框架设计的灵魂,本文通过例子能看的更加清晰的理解
简介:
Java的反射(reflection)机制是指在程序的运行状态中,可以构造任意一个类的对象,可以了解任意一个对象所属的类,可以了解任意一个类的成员变量和方法,可以调用任意一个对象的属性和方法。这种动态获取程序信息以及动态调用对象的功能称为Java语言的反射机制。反射被视为动态语言的关键。
1、反射:通过对象反射出一个类
package com.example.java.javakuangstudy.Reflection; /** * 反射练习-->得到class类的几种方式 */ public class Text01 { public static void main(String[] args) throws ClassNotFoundException { Person person = new Student(); System.out.println("我这个人是:" + person.name); //TODO 方式一:通过对象获取 Class<? extends Person> c1 = person.getClass(); System.out.println(c1.hashCode()); //TODO 方式二: Class<?> c2 = Class.forName("com.example.java.javakuangstudy.Reflection.Student"); System.out.println(c2.hashCode()); //TODO 方式三:通过类名.class获取 Class<Student> c3 = Student.class; System.out.println(c3.hashCode()); //TODO 方式四:基本内置类型的包装类都有一个type属性 Class<Integer> c4 = Integer.TYPE; System.out.println(c4); //TODO 获取父类类型 Class<?> c5 = c1.getSuperclass(); System.out.println(c5); } } class Person { public String name; public Person(String name) { this.name = name; } public Person() { } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } } class Student extends Person { public Student() { this.name = "学生"; } } class Teacher extends Person { public Teacher() { this.name = "老师"; } }
结果
我这个人是:学生
834600351
834600351
834600351
intclass com.example.java.javakuangstudy.Reflection.Person
2、分析类的初始化
package com.example.java.javakuangstudy.Reflection; /** * 测试类什么时候会初始化 */ public class Text02 { static { System.out.println("Main被加载"); } public static void main(String[] args) throws ClassNotFoundException { //TODO 通过new 主动引用 //Son son = new Son(); //TODO 通过反射也会产生主动引用 //Class.forName("com.example.java.javakuangstudy.Reflection.Son"); //TODO 不会产生类的引用的方法 //TODO 1、子类调用父类的静态方法 //int b = Son.b; //System.out.println(b); // TODO 2、通过数组 //Son[] arr = new Son[5]; //只打印--- Main被加载 (父类子类都不加载) //TODO 3、调用常量 System.out.println(Son.M); } } class Father { static int b = 2; static { System.out.println("父类被加载"); } } class Son extends Father { static { System.out.println("子类被加载"); m = 300; } static int m = 100; static final int M = 1; }
3、动态的创建对象 通过反射
package com.example.java.javakuangstudy.Reflection; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 动态的创建对象 通过反射 */ @SuppressWarnings("all") public class Text03 { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { //TODO 获得class对象 Class c1 = Class.forName("com.example.java.javakuangstudy.Reflection.User"); //TODO 构造一个对象 User user = (User) c1.newInstance(); //本质是调用了类的无参构造 System.out.println(user); //TODO 通过构造器创建对象 Constructor constructor = c1.getDeclaredConstructor(String.class, int.class, int.class); User user1 = (User) constructor.newInstance("liulianglin", 24, 251314); System.out.println(user1); //TODO 通过反射调用普通方法 User user2 = (User) c1.newInstance(); //TODO 通过反射获取一个方法 Method setName = c1.getDeclaredMethod("setName", String.class); //TODO invoke激活的意思 (对象,"方法的值") setName.invoke(user2, "刘亮林"); System.out.println(user2.getName()); //TODO 通过反射操作方法 System.out.println("----------------------"); User user3 = (User) c1.newInstance(); Field name = c1.getDeclaredField("name"); //TODO 不能直接操作私有属性,我们需要关闭程序的安全检测,将setAccessible(true) 可提高效率 name.setAccessible(true); name.set(user3, "liulianglin"); System.out.println(user3.getName()); } }
4、 练习反射操作注解
package com.example.java.javakuangstudy.Reflection; import java.lang.annotation.*; import java.lang.reflect.Field; /** * 练习反射操作注解 */ @SuppressWarnings("all") public class Text04 { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class c1 = Class.forName("com.example.java.javakuangstudy.Reflection.Student2"); //TODO 通过反射获取全部注解 Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); //@com.example.java.javakuangstudy.Reflection.Tablekuang(value=db_student) } //TODO 获得注解的value值 Tablekuang tablekuang = (Tablekuang) c1.getAnnotation(Tablekuang.class); String value = tablekuang.value(); System.out.println(value); //db_student //TODO 获得类指定的注解 Field name = c1.getDeclaredField("id"); Fieldkuang annotation = name.getAnnotation(Fieldkuang.class); System.out.println(annotation.columnName()); //db_id System.out.println(annotation.type()); //int System.out.println(annotation.length()); //18 } } @Tablekuang("db_student") class Student2 { @Fieldkuang(columnName = "db_id", type = "int", length = 18) private int id; @Fieldkuang(columnName = "db_age", type = "int", length = 18) private int age; @Fieldkuang(columnName = "db_name", type = "varchar", length = 18) private String name; public Student2() { } public Student2(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student2{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + '}'; } } //TODO 类名的注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface Tablekuang { String value(); } //TODO 属性的注解 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Fieldkuang { String columnName(); String type(); int length(); }
到此这篇关于Java 反射机制的文章就介绍到这了,更多相关Java反射内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!