{"id":1373,"date":"2022-11-01T22:57:07","date_gmt":"2022-11-01T14:57:07","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1373"},"modified":"2022-11-27T12:39:45","modified_gmt":"2022-11-27T04:39:45","slug":"%e5%8f%8d%e5%b0%84-%e9%80%9a%e8%bf%87%e5%8f%8d%e5%b0%84%ef%bc%8c%e5%88%9b%e5%bb%ba%e8%bf%90%e8%a1%8c%e6%97%b6%e7%b1%bb%e7%9a%84%e5%af%b9%e8%b1%a1%e3%80%81%e4%b8%be%e4%be%8b%e4%bd%93%e4%bc%9a%e5%8f%8d","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1373","title":{"rendered":"\u53cd\u5c04-\u901a\u8fc7\u53cd\u5c04\uff0c\u521b\u5efa\u8fd0\u884c\u65f6\u7c7b\u7684\u5bf9\u8c61\u3001\u4e3e\u4f8b\u4f53\u4f1a\u53cd\u5c04\u7684\u52a8\u6001\u6027"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P647;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.util.Random;\r\n\r\n\/**\r\n * \u901a\u8fc7\u53cd\u5c04\u521b\u5efa\u5bf9\u5e94\u7684\u8fd0\u884c\u65f6\u7c7b\u7684\u5bf9\u8c61\r\n *\r\n * @author RichieZhang\r\n * @create 2022-11-01 \u4e0b\u5348 6:47\r\n *\/\r\npublic class NewInstanceTest {\r\n\r\n    @Test\r\n    public void test1() throws InstantiationException, IllegalAccessException {\r\n\r\n        Class&lt;Person> clazz = Person.class;\r\n\r\n        \/*\r\n        \/\/newInstance(): \u8c03\u7528\u6b64\u65b9\u6cd5\uff0c\u521b\u5efa\u5bf9\u5e94\u7684\u8fd0\u884c\u65f6\u7c7b\u7684\u5bf9\u8c61\u3002\u5185\u90e8\u8c03\u7528\u4e86\u8fd0\u884c\u65f6\u7c7b\u7684\u7a7a\u53c2\u6784\u9020\u5668\u3002\r\n\r\n        \u8981\u60f3\u6b64\u65b9\u6cd5\u6b63\u5e38\u7684\u521b\u5efa\u8fd0\u884c\u65f6\u7c7b\u7684\u5bf9\u8c61\uff0c\u8981\u6c42\uff1a\r\n        1. \u8fd0\u884c\u65f6\u7c7b\u5fc5\u987b\u63d0\u4f9b\u7a7a\u53c2\u7684\u6784\u9020\u5668\r\n        2. \u7a7a\u53c2\u7684\u6784\u9020\u5668\u7684\u8bbf\u95ee\u6743\u9650\u5f97\u591f\u3002\u901a\u5e38\uff0c\u8bbe\u7f6e\u4e3apublic\u3002\r\n\r\n        \u5728javabean\u4e2d\u8981\u6c42\u63d0\u4f9b\u4e00\u4e2apublic\u7684\u7a7a\u53c2\u6784\u9020\u5668\u3002\u539f\u56e0\uff1a\r\n        1. \u4fbf\u4e8e\u901a\u8fc7\u53cd\u5c04\uff0c\u521b\u5efa\u8fd0\u884c\u65f6\u7c7b\u7684\u5bf9\u8c61\r\n        2. \u4fbf\u4e8e\u5b50\u7c7b\u7ee7\u627f\u6b64\u8fd0\u884c\u7c7b\u65f6\uff0c\u9ed8\u8ba4\u8c03\u7528super()\u65f6\uff0c\u4fdd\u8bc1\u7236\u7c7b\u6709\u6b64\u6784\u9020\u5668\r\n\r\n         *\/\r\n        Person obj = clazz.newInstance();\r\n\/\/        \u6839\u636eJAVA11\u7684API \u6211\u4eec\u53ef\u4ee5\u770b\u89c1\u53cd\u5c04\u4e2d\u7684newInstance\uff08\uff09\u65b9\u6cd5\u4e0d\u63a8\u8350\u4f7f\u7528\u4e86\uff0c\u7528\r\n\/\/        clazz.getDeclaredConstructor().newInstance()\u4ee3\u66ff\r\n\/\/        Object obj = clazz.getDeclaredConstructor().newInstance();\r\n\r\n        System.out.println(obj);\r\n\r\n    }\r\n\r\n    \/\/\u4f53\u4f1a\u53cd\u5c04\u7684\u52a8\u6001\u6027\r\n    @Test\r\n    public void test2(){\r\n\r\n        for (int i = 0; i &lt; 100; i++) {\r\n            int num = new Random().nextInt(3);\/\/0,1,2\r\n            String classPath = \"\";\r\n            switch (num){\r\n                case 0:\r\n                    classPath = \"java.util.Date\";\r\n                    break;\r\n                case 1:\r\n                    classPath = \"java.lang.Object\";\r\n                    break;\r\n                case 2:\r\n                    classPath = \"top.qaqaq.java.P647.Person\";\r\n                    break;\r\n            }\r\n\r\n            try {\r\n                Object obj = getInstance(classPath);\r\n                System.out.println(obj);\r\n            } catch (Exception e) {\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n\r\n    }\r\n\r\n    \/*\r\n    \u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u7c7b\u7684\u5bf9\u8c61\u3002\r\n    classPath: \u6307\u5b9a\u7c7b\u7684\u5168\u7c7b\u540d\r\n     *\/\r\n\r\n    public Object getInstance(String classPath) throws Exception {\r\n        Class Clazz = Class.forName(classPath);\r\n        return Clazz.newInstance();\r\n    }\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P647;\r\n\r\n\/**\r\n * @author RichieZhang\r\n * @create 2022-11-01 \u4e0b\u5348 2:34\r\n *\/\r\npublic class Person {\r\n\r\n    private String name;\r\n    public int age;\r\n\r\n    public Person() {\r\n        System.out.println(\"Person()\");\r\n    }\r\n\r\n    public Person(String name) {\r\n        this.name = name;\r\n    }\r\n\r\n    public Person(String name, int age) {\r\n        this.name = name;\r\n        this.age = age;\r\n    }\r\n\r\n    public String getName() {\r\n        return name;\r\n    }\r\n\r\n    public void setName(String name) {\r\n        this.name = name;\r\n    }\r\n\r\n    public int getAge() {\r\n        return age;\r\n    }\r\n\r\n    public void setAge(int age) {\r\n        this.age = age;\r\n    }\r\n\r\n    @Override\r\n    public String toString() {\r\n        return \"Person{\" +\r\n                \"name='\" + name + '\\'' +\r\n                \", age=\" + age +\r\n                '}';\r\n    }\r\n\r\n    public void show(){\r\n        System.out.println(\"\u4f60\u597d\uff0c\u6211\u662f\u4e00\u4e2a\u4eba\");\r\n    }\r\n\r\n    private String showNation(String nation){\r\n        System.out.println(\"\u6211\u7684\u56fd\u7c4d\u662f\" + nation);\r\n        return nation;\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1373","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1373"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1373"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1373\/revisions"}],"predecessor-version":[{"id":1374,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1373\/revisions\/1374"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}