1、动态创建对象

public class test2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Class c1 = Class.forName("com.jia.util.person");

        //构建一个对象
        person p1 = (person)c1.newInstance();  //person必须要有无参构造,本质时调用类的无参构造
        System.out.println(p1);

        Constructor declaredConstructor = c1.getDeclaredConstructor(String.class); ///获取有参构造器
        person p2 = (person)declaredConstructor.newInstance("张三");//通过有参构造
        System.out.println(p2);

        //操纵方法
        Method declaredMethod = c1.getDeclaredMethod("setName", String.class);
        declaredMethod.invoke(p1,"李四");//激活  这个方法
        System.out.println(p1);

        //操作属性
        Field name = c1.getDeclaredField("name");
        name.setAccessible(true); //私有属性不能直接操作,需要关闭安全检测
        name.set(p1,"王五");
        System.out.println(p1);
    }
}

2、性能测试

  • 普通方法最短
  • 反射方式最长
  • 关闭检测可以调高使用注解的效率

3、操作泛型

  • Java采用泛型擦除的机制来引入泛型 , Java中的泛型仅仅是给编译器javac使用的,确保数据的安全性 和免去强制类型转换问题 , 但是 , 一旦编译完成 , 所有和泛型有关的类型全部擦除
  • 为了通过反射操作这些类型 , Java新增了 ParameterizedType , GenericArrayType , TypeVariable 和 WildcardType 几种类型来代表不能被归一到Class类中的类型但是又和原始类型齐名的类型.
  • ParameterizedType : 表示一种参数化类型,比如Collection
  • GenericArrayType : 表示一种元素类型是参数化类型或者类型变量的数组类型
  • TypeVariable : 是各种类型变量的公共父接口
  • WildcardType : 代表一种通配符类型表达式
//通过反射操作泛型
public class test3 {
    public void test01(Map<String,person> map){
        System.out.println("test01");

    }
    public Map<String,person> test02(){
        System.out.println("test02");
        return null;
    }

    public static void main(String[] args) throws NoSuchMethodException {


        Method test01 = test3.class.getMethod("test01", Map.class);
        Type[] genericParameterTypes = test01.getGenericParameterTypes(); //获得所有输入类型
        for (Type genericParameterType : genericParameterTypes) {
            System.out.println(genericParameterType);
            if(genericParameterType instanceof ParameterizedType){    //如果时参数化类型
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();//强转成真实类型
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);//输出
                }
            }
        }


        Method test02 = test3.class.getMethod("test02");
        Type genericReturnType = test02.getGenericReturnType();  //获得返回值参数类型
        if(genericReturnType instanceof ParameterizedType){   //如果时参数化类型
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments(); //强转成真实类型
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument);//输出
            }
        }

    }
}

4、操作注解

  • getAnnotations
  • getAnnotation
Object relationship Mapping --> 对象关系映射ORP
//反射操作注解
public class test4 {
    public static void main(String[] args) throws NoSuchFieldException {
        Class<student> c1    = student.class; //获得反射
        Annotation[] annotations = c1.getAnnotations(); //获得所有注解
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        myclass annotation = c1.getAnnotation(myclass.class);  //获得注解
        System.out.println(annotation.value());

        Field name = c1.getDeclaredField("name");//获得注解
        myfiled annotation1 = name.getAnnotation(myfiled.class);
        System.out.println(annotation1.value());

    }

}
@myclass("类注解")
class student {

    @myfiled("属性注解1")
    private String name;
    @myfiled("属性注解2")
    private int age;



    public student() {
    }
    public student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface myclass{
    String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface myfiled{
    String value();
}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐