13.动态创建对象执行方法
1. 创建类的对象: 调用class对象的newInstance()方法 (1).类必须有一个无参数的构造器 (2).类的构造器的访问权限需要足够 eg: User user = (User) aClass.newInstance(); 构造一个对象----->本质上调用了无参 2.难道没有无参的构造器就不能创建对象了么? (1).只要在操作的时候明确的调用类中的构造器,并将参数传递进去之后, 才可以实列化.
2.没有无参构造器的解决办法: 这里User没有添加: 无参
package com.Kuang.reflectiom; public class Demo5 { public static void main(String[] args) throws ClassCastException { //通过反射获取类的class对象 try { Class aClass_one = Class.forName("com.Kuang.reflectiom.User"); System.out.println(aClass_one); //一个类在内存中只有一个class对象 //一个类被加载后,类的整个结构都会被封装在class对象中 Class aClass_2 = Class.forName("com.Kuang.reflectiom.User"); Class aClass_3 = Class.forName("com.Kuang.reflectiom.User"); Class aClass_4 = Class.forName("com.Kuang.reflectiom.User"); System.out.println(aClass_2.hashCode()); System.out.println(aClass_3.hashCode()); System.out.println(aClass_4.hashCode()); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } //实体类 pojo,entity class User{ private String name; private int id; private int age; public User(String name, int id, int age) { this.name = name; this.id = id; this.age = age; } public String getName() { return name; } public void setName(String name) { 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; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", id=" + id + ", age=" + age + '}'; } }
package com.Kuang.reflectiom; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Demo12 { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { //获得class对象 Class aClass = Class.forName("com.Kuang.reflectiom.User"); //获得构造器 ,通过构造器创建对象----->调用了有参 Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class, int.class, int.class); User user2 = (User)declaredConstructor.newInstance("吉士先生", 001, 18); System.out.println(user2); } }
1.执行方法: setAccessible(true); 关闭权限检测 invoke(user3,"吉士先生") invoke: 激活的意思 (对象,"方法的值")
package com.Kuang.reflectiom; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Demo12 { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { //获得class对象 Class aClass = Class.forName("com.Kuang.reflectiom.User"); //构造一个对象----->调用了无参 User user = (User) aClass.newInstance(); System.out.println(user); //获得构造器 ,通过构造器创建对象----->调用了有参 Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class, int.class, int.class); User user2 = (User)declaredConstructor.newInstance("吉士先生", 001, 18); System.out.println(user2); //通过反射调用方法 User user3=(User) aClass.newInstance(); Method setName = aClass.getDeclaredMethod("setName", String.class); //invoke: 激活的意思 (对象,"方法的值") setName.invoke(user3,"吉士先生"); System.out.println(user3.getName()); //通过反射操作属性 User user4=(User) aClass.newInstance(); Field name = aClass.getDeclaredField("name"); name.setAccessible(true); //关闭权限检测,如果不关闭,那么私有属性我们就访问不了 name.set(user4,"吉士先生2"); System.out.println(user4.getName()); } }
14.获取泛型信息
1.java采用泛型擦除的机制来引入泛型,java中的泛型仅仅是给编译器javac使用的, 确保数据的安全性和免去强制类型转换问题,但是,一旦编译完成,所有和泛型有关 的类型全部擦除。 2.为了通过反射操作这些类型,java新增了parameterizedTpye,GenericType, TypeVaiable和WildcardType几种类型来代表不能被归一到class类中的类型但是 又和原始类型齐名的类型 (1).parameterizedTpye: 表示一种参数化类型 (2).GenericType: 表示一种元素类型是参数化类型或者类型变量的数组类型 (3).TypeVaiable: 是各种类型变量的公共父接口 (4).WildcardType: 代表一种通配符类型表达式。
15.反射操作注解
package com.Kuang.reflectiom; import java.lang.annotation.*; import java.lang.reflect.Field; public class Demo13 { public static void main(String[] args) throws NoSuchFieldException { try { Class<?> aClass = Class.forName("com.Kuang.reflectiom.Student2"); //通过反射获得注解 Annotation[] annotations = aClass.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } //获得注解的value的值 TableKuang tableKuang = (TableKuang) aClass.getAnnotation(TableKuang.class); String value = tableKuang.value(); System.out.println(value); //获得类指定的注解 Field name = aClass.getDeclaredField("name"); FieldKuang annotation = name.getAnnotation(FieldKuang.class); System.out.println(annotation.columname()); System.out.println(annotation.type()); System.out.println(annotation.length()); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } @TableKuang("db_student") class Student2{ @FieldKuang(columname = "db_id",type = "int",length = 10) private int id; @FieldKuang(columname = "db_age",type = "int",length = 10) private int age; @FieldKuang(columname = "db_name",type = "Varchar",length = 10) private String name; public Student2() { } public Student2(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } @Override public String toString() { return "com.Kuang.reflectiom.Student2{" + "id=" + id + ", age=" + age + ", 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; } } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface TableKuang{ String value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface FieldKuang{ String columname(); String type(); int length(); }