反射-通过反射,创建运行时类的对象、举例体会反射的动态性
package top.qaqaq.java.P647;

import org.junit.jupiter.api.Test;

import java.util.Random;

/**
 * 通过反射创建对应的运行时类的对象
 *
 * @author RichieZhang
 * @create 2022-11-01 下午 6:47
 */
public class NewInstanceTest {

    @Test
    public void test1() throws InstantiationException, IllegalAccessException {

        Class<Person> clazz = Person.class;

        /*
        //newInstance(): 调用此方法,创建对应的运行时类的对象。内部调用了运行时类的空参构造器。

        要想此方法正常的创建运行时类的对象,要求:
        1. 运行时类必须提供空参的构造器
        2. 空参的构造器的访问权限得够。通常,设置为public。

        在javabean中要求提供一个public的空参构造器。原因:
        1. 便于通过反射,创建运行时类的对象
        2. 便于子类继承此运行类时,默认调用super()时,保证父类有此构造器

         */
        Person obj = clazz.newInstance();
//        根据JAVA11的API 我们可以看见反射中的newInstance()方法不推荐使用了,用
//        clazz.getDeclaredConstructor().newInstance()代替
//        Object obj = clazz.getDeclaredConstructor().newInstance();

        System.out.println(obj);

    }

    //体会反射的动态性
    @Test
    public void test2(){

        for (int i = 0; i < 100; i++) {
            int num = new Random().nextInt(3);//0,1,2
            String classPath = "";
            switch (num){
                case 0:
                    classPath = "java.util.Date";
                    break;
                case 1:
                    classPath = "java.lang.Object";
                    break;
                case 2:
                    classPath = "top.qaqaq.java.P647.Person";
                    break;
            }

            try {
                Object obj = getInstance(classPath);
                System.out.println(obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /*
    创建一个指定类的对象。
    classPath: 指定类的全类名
     */

    public Object getInstance(String classPath) throws Exception {
        Class Clazz = Class.forName(classPath);
        return Clazz.newInstance();
    }
}
package top.qaqaq.java.P647;

/**
 * @author RichieZhang
 * @create 2022-11-01 下午 2:34
 */
public class Person {

    private String name;
    public int age;

    public Person() {
        System.out.println("Person()");
    }

    public Person(String name) {
        this.name = name;
    }

    public Person(String name, int age) {
        this.name = name;
        this.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;
    }

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

    public void show(){
        System.out.println("你好,我是一个人");
    }

    private String showNation(String nation){
        System.out.println("我的国籍是" + nation);
        return nation;
    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇