{"id":6888,"date":"2022-08-08T10:14:33","date_gmt":"2022-08-08T02:14:33","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6888"},"modified":"2022-08-08T13:07:39","modified_gmt":"2022-08-08T05:07:39","slug":"java%e4%bd%bf%e7%94%a8%e5%8f%8d%e5%b0%84%e5%88%9b%e5%bb%ba%e8%a2%abprivate%e4%bf%ae%e9%a5%b0%e7%9a%84%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6888","title":{"rendered":"Java\u4f7f\u7528\u53cd\u5c04\u521b\u5efa\u88abprivate\u4fee\u9970\u7684\u6784\u9020\u51fd\u6570\u5bf9\u8c61"},"content":{"rendered":"\n<p>\u5982\u679c\u4e00\u4e2a<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0&amp;spm=1001.2101.3001.7020\" target=\"_blank\">\u6784\u9020\u51fd\u6570<\/a>\u662fpublic\u7684\u8bdd\uff0c\u90a3\u4e48\u6211\u4eec\u5229\u7528\u53cd\u5c04\u662f\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u7684\u6784\u9020\u51fd\u6570\u662f\u88abprivate\u4fee\u9970\u7684\u8bdd\uff0c\u90a3\u4e48\u7528\u53cd\u5c04\u8fd8\u80fd\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5417\uff1f\u7b54\u6848\u662f\u53ef\u4ee5\u7684\uff0c\u6ce8\u610f\u4e00\u70b9\u5c31\u53ef\u4ee5\u62ff\u5230\u4e86\uff0c\u4e0b\u9762\u4e00\u8d77\u6765\u770b\u770b\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u6211\u4eec\u6765\u521b\u5efa\u4e00\u4e2a\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class PrivateTest {\n\n    private int num;\n\n    private PrivateTest() {\n          num = 1\n    }\n\n   private PrivateTest(int num) {\n          this.num = num\n   }\n\n    public void output() {\n        System.out.print(\"string is : me\" );\n    }\n}<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\u662f<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=private&amp;spm=1001.2101.3001.7020\" target=\"_blank\">private<\/a>\u4fee\u9970\u7684\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u5148\u7528newInstance\u65b9\u6cd5\u8bd5\u8bd5\uff0c\u770b\u80fd\u4e0d\u80fd\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static void main(String[] args) {\n    try {\n        Class clz = Class.forName(\"reflectDemo.PrivateTest\");\n        Object obj = clz.newInstance();\n        if (obj instanceof PrivateTest){\n            PrivateTest test = (PrivateTest) obj;\n            test.output();\n        }\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n}<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7a0b\u5e8f\u4f1a\u53d1\u73b0\u62a5\u9519\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39-1024x138.png\" alt=\"\" class=\"wp-image-6889\" width=\"714\" height=\"96\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39-1024x138.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39-300x40.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39-768x103.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39-1536x206.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39-830x112.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39-230x31.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39-350x47.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39-480x64.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-39.png 1846w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u8fd9\u79cd\u65b9\u5f0f\u884c\u4e0d\u901a\uff0c\u90a3\u4e48\u6211\u4eec\u7528Constructor\u6765\u8bd5\u8bd5\u770b\u884c\u4e0d\u884c\u5462<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static void main(String[] args) {\n    try {\n        Class clz = Class.forName(\"reflectDemo.PrivateTest\");\n        Constructor constructor = clz.getDeclaredConstructor();\n        Object obj = constructor.newInstance();\n        if (obj instanceof PrivateTest){\n            PrivateTest test = (PrivateTest) obj;\n            test.output();\n        }\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n}<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7a0b\u5e8f\u540c\u6837\u4e5f\u4f1a\u62a5\u9519\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40-1024x181.png\" alt=\"\" class=\"wp-image-6890\" width=\"720\" height=\"127\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40-1024x181.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40-300x53.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40-768x136.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40-1536x272.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40-830x147.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40-230x41.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40-350x62.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40-480x85.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-40.png 1842w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u9519\u8bef\u7684\u95ee\u9898\u90fd\u662f\u4e00\u6837\u7684\uff0c\u90a3\u5c31\u662f\u6211\u4eec\u4e0d\u80fd\u8bbf\u95eeprivate\u4fee\u9970\u7684\u6784\u9020\u51fd\u6570\uff0c\u90a3\u4e48\u600e\u4e48\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f<\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6cd5\u8fd8\u662f\u4f7f\u7528Constructor\uff0c\u4e0d\u8fc7\u9700\u8981\u7ed9\u4ee3\u7801\u591a\u52a0\u4e00\u884c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    constructor.setAccessible(true);<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static void main(String[] args) {\n    try {\n        Class clz = Class.forName(\"reflectDemo.PrivateTest\");\n        \/\/ getDeclaredConstructor\u4e0d\u52a0\u53c2\u6570\u7684\u8bdd\uff0c\u662f\u53d6\u5f97\u65e0\u53c2\u6570\u6784\u9020\u5668\n        Constructor constructor = clz.getDeclaredConstructor();\n        \/\/ \u5982\u679c\u60f3\u53d6\u5f97\u6709\u53c2\u6570\u7684\u6784\u9020\u5668\uff0c\u9700\u8981\u628a\u6307\u5b9a\u53c2\u6570\u7684Class\u5bf9\u8c61\u52a0\u4e0a\n        \/\/ Constructor constructor = clz.getDeclaredConstructor(int.class);\n        constructor.setAccessible(true);\n        Object obj = constructor.newInstance();\n        if (obj instanceof PrivateTest){\n            PrivateTest test = (PrivateTest) obj;\n            test.output();\n        }\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n}\n<\/pre>\n\n\n\n<p>\u52a0\u4e0a\u8fd9\u884c\u4ee3\u7801\u7684\u542b\u4e49\u8868\u793a\u6211\u4eec\u7684constructor\u5bf9\u8c61\u80fd\u591f\u8bbf\u95ee\u6211\u4eec\u7684private\u4fee\u9970\u7684\u6784\u9020\u51fd\u6570\uff0c\u8fd0\u884c\u7a0b\u5e8f\u770b\u770b\u53d8\u5316\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-41.png\" alt=\"\" class=\"wp-image-6891\" width=\"472\" height=\"156\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-41.png 692w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-41-300x99.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-41-230x76.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-41-350x115.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-41-480x158.png 480w\" sizes=\"(max-width: 472px) 100vw, 472px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u7684\u7a0b\u5e8f\u5df2\u7ecf\u80fd\u591f\u901a\u8fc7<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E5%8F%8D%E5%B0%84&amp;spm=1001.2101.3001.7020\" target=\"_blank\">\u53cd\u5c04<\/a>\u7684\u5f62\u5f0f\u521b\u5efa\u4e00\u4e2a\u88abprivate\u4fee\u9970\u7684\u6784\u9020\u51fd\u6570\u5bf9\u8c61\u5e76\u4e14\u80fd\u591f\u6b63\u786e\u7684\u8c03\u7528\u5bf9\u8c61\u91cc\u7684\u65b9\u6cd5\uff0c\u6240\u4ee5\u5229\u7528\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u6211\u4eec\u7684\u95ee\u9898\uff0c\u5c0f\u77e5\u8bc6\u70b9\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u5e2e\u52a9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u662fpublic\u7684\u8bdd\uff0c\u90a3\u4e48\u6211\u4eec\u5229\u7528\u53cd\u5c04\u662f\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u7684\u6784\u9020\u51fd\u6570\u662f\u88ab [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6888"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6888"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6888\/revisions"}],"predecessor-version":[{"id":6895,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6888\/revisions\/6895"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}