返回单个构造方法

private static void method1() throws ClassNotFoundException, NoSuchMethodException {
    Class clazz = Class.forName("com.baidu.fs.Student");
    Constructor constructor = clazz.getDeclaredConstructor(String.class);
    System.out.println(constructor);
}

返回所有构造方法数组

private static void method2() throws ClassNotFoundException {
    Class clazz = Class.forName("com.baidu.fs.Student");
        Constructor[] constructors = clazz.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
        System.out.println(constructor);
    }
}

返回单个公共的构造方法

private static void method3() throws ClassNotFoundException, NoSuchMethodException {
    Class clazz = Class.forName("com.baidu.fs.Student");
    Constructor constructor = clazz.getConstructor();
    System.out.println(constructor);

    Constructor constructor1 = clazz.getConstructor(String.class, int.class);
    System.out.println(constructor1);
}

返回所有公共的构造方法数组

private static void method4() throws ClassNotFoundException {
    Class clazz = Class.forName("com.baidu.fs.Student");
    Constructor[] constructors = clazz.getConstructors();
    for (Constructor constructor : constructors) {
        System.out.println(constructor);
    }
}

返回一个私有构造方法并创建的对象

private static void method5() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    Class clazz = Class.forName("com.baidu.fs.Student");
    Constructor constructor = clazz.getDeclaredConstructor(String.class);
    //取消访问检查 非public
    constructor.setAccessible(true);
    Student student = (Student) constructor.newInstance("zhangshan");
    System.out.println(student);
}

返回一个构造方法并创建的对象

private static void method6() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    Class clazz = Class.forName("com.baidu.fs.Student");
    Constructor constructor = clazz.getDeclaredConstructor(String.class,int.class);
    Student student = (Student) constructor.newInstance("zhangsan", 22);
    System.out.println(student);
}

返回一个构造方法并创建的对象

private static void method7() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    Class clazz = Class.forName("com.baidu.fs.Student");
    Constructor constructor = clazz.getDeclaredConstructor();
    Student student = (Student) constructor.newInstance();
    System.out.println(student);
}

返回单个成员变量对象

private static void method8() throws ClassNotFoundException, NoSuchFieldException {
    Class clazz = Class.forName("com.baidu.fs.Student");
    Field money = clazz.getDeclaredField("money");
    System.out.println(money);
}

返回单个公共成员变量对象

private static void method9() throws ClassNotFoundException, NoSuchFieldException {
    Class clazz = Class.forName("com.baidu.fs.Student");
    Field field = clazz.getField("age");
    System.out.println(field);
}

返回所有功能成员变量对象

private static void method10() throws ClassNotFoundException {
    Class clazz = Class.forName("com.baidu.fs.Student");
    Field[] fields = clazz.getFields();
    for (Field field : fields) {
        System.out.println(field);
    }
}

set方法对成员对象进行赋值

private static void method11() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    //获取class对象
    Class clazz = Class.forName("com.baidu.fs.Student");
    //获取name这个field对象
    Field field = clazz.getField("name");
    //创建对象
    Constructor con = clazz.getConstructor();
    Student student = (Student) con.newInstance();
    //给指定对象赋值
    field.set(student,"zhangsan");
    System.out.println(student);
}

获取一个成员对象

private static void method12() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    //获取class对象
    Class clazz = Class.forName("com.baidu.fs.Student");
    //获取成员对象
    Field field = clazz.getDeclaredField("money");
    //取消访问检查
    field.setAccessible(true);
    //创建一个对象
    Constructor con = clazz.getConstructor();
    Student student = (Student) con.newInstance();
    System.out.println(student);
}

获取一个带参的成员方法

private static void method13() throws ClassNotFoundException, NoSuchMethodException {
    //获取class对象
    Class clazz = Class.forName("com.baidu.fs.Student");
    //获取一个有参的成员方法 公开的
    Method method = clazz.getMethod("run",int.class);
    System.out.println(method);
}

获取一个无参的成员方法 必须是公开的

private static void method14() throws ClassNotFoundException, NoSuchMethodException {
    //获取class对象
    Class clazz = Class.forName("com.baidu.fs.Student");
    //获取一个无参的成员方法 必须是公开的
    Method play = clazz.getMethod("play");
    System.out.println(play);
}

获取非公开的 无参数的 成员方法

private static void method15() throws ClassNotFoundException, NoSuchMethodException {
    //获取class对象
    Class clazz = Class.forName("com.baidu.fs.Student");
    //获取非公开 无参数 的成员方法
    Method constructor = clazz.getDeclaredMethod("tv");
    System.out.println(constructor);
}

获取非公开的 有参数的 成员方法

private static void method16() throws ClassNotFoundException, NoSuchMethodException {
    //获取class对象
    Class clazz = Class.forName("com.baidu.fs.Student");
    //获取非公开 有参数 的成员方法
    Method constructor = clazz.getDeclaredMethod("sleep",int.class);
    System.out.println(constructor);
}

返回所有成员方法对象的数组 不包括继承的

private static void method17() throws ClassNotFoundException {
    //获取class对象
    Class clazz = Class.forName("com.baidu.fs.Student");
    //返回所有成员方法对象的数组 不包括继承的
    Method[] constructor = clazz.getDeclaredMethods();
    for (Method method : constructor) {
        System.out.println(method);
    }
}

返回所有成员方法对象的数组 包括继承的 必须是公开的

private static void method18() throws ClassNotFoundException {
    //获取class对象
    Class clazz = Class.forName("com.baidu.fs.Student");
    //返回所有成员方法对象的数组 包括继承的  必须是公开的
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        System.out.println(method);
    }
}
最后修改:2022 年 01 月 24 日
如果觉得我的文章对你有用,请随意赞赏