{"id":221388,"date":"2021-07-07T08:50:03","date_gmt":"2021-07-07T00:50:03","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=221388"},"modified":"2021-06-25T09:50:34","modified_gmt":"2021-06-25T01:50:34","slug":"introduction-to-internal","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/introduction-to-internal.html","title":{"rendered":"java \u4e2d\u7684\u5185\u90e8\u7c7b\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5185\u90e8\u7c7b\u4e0d\u662f\u5f88\u597d\u7406\u89e3\uff0c\u4f46\u8bf4\u767d\u4e86\u5176\u5b9e\u4e5f\u5c31\u662f\u4e00\u4e2a\u7c7b\u4e2d\u8fd8\u5305\u542b\u7740\u53e6\u5916\u4e00\u4e2a\u7c7b\u3002\u5982\u540c\u4e00\u4e2a\u4eba\u662f\u7531\u5927\u8111\u3001\u80a2\u4f53\u3001\u5668\u5b98\u7b49\u8eab\u4f53\u7ed3\u679c\u7ec4\u6210\uff0c\u800c\u5185\u90e8\u7c7b\u76f8\u5f53\u4e8e\u5176\u4e2d\u7684\u67d0\u4e2a\u5668\u5b98\u4e4b\u4e00\uff0c\u4f8b\u5982\u5fc3\u810f\uff1a\u5b83\u4e5f\u6709\u81ea\u5df1\u7684\u5c5e\u6027\u548c\u884c\u4e3a\uff08\u8840\u6db2\u3001\u8df3\u52a8\uff09\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u663e\u7136\uff0c\u6b64\u5904\u4e0d\u80fd\u5355\u65b9\u9762\u7528\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\u8868\u793a\u4e00\u4e2a\u5fc3\u810f\uff0c\u800c\u9700\u8981\u4e00\u4e2a\u7c7b\u3002<\/p>\n

\u800c\u5fc3\u810f\u53c8\u5728\u4eba\u4f53\u5f53\u4e2d\uff0c\u6b63\u5982\u540c\u662f\u5185\u90e8\u7c7b\u5728\u5916\u90e8\u5185\u5f53\u4e2d\u3002<\/p>\n

\u5b9e\u4f8b1\uff1a\u5185\u90e8\u7c7b\u7684\u57fa\u672c\u7ed3\u6784<\/strong><\/div>\n
\/\/\u5916\u90e8\u7c7b\r\nclass Out {\r\n    private int age = 12;\r\n     \r\n    \/\/\u5185\u90e8\u7c7b\r\n    class In {\r\n        public void print() {\r\n            System.out.println(age);\r\n        }\r\n    }\r\n}\r\n \r\npublic class Demo {\r\n    public static void main(String[] args) {\r\n        Out.In in = new Out().new In();\r\n        in.print();\r\n        \/\/\u6216\u8005\u91c7\u7528\u4e0b\u79cd\u65b9\u5f0f\u8bbf\u95ee\r\n        \/*\r\n        Out out = new Out();\r\n        Out.In in = out.new In();\r\n        in.print();\r\n        *\/\r\n    }\r\n}<\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

12<\/pre>\n

\u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u4e0d\u96be\u770b\u51fa\uff0c\u5185\u90e8\u7c7b\u5176\u5b9e\u4e25\u91cd\u7834\u574f\u4e86\u826f\u597d\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u4f46\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528\u5185\u90e8\u7c7b\u5462\uff1f<\/p>\n

\u56e0\u4e3a\u5185\u90e8\u7c7b\u53ef\u4ee5\u968f\u610f\u4f7f\u7528\u5916\u90e8\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff08\u5305\u62ec\u79c1\u6709\uff09\u800c\u4e0d\u7528\u751f\u6210\u5916\u90e8\u7c7b\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e5f\u662f\u5185\u90e8\u7c7b\u7684\u552f\u4e00\u4f18\u70b9\u3002<\/p>\n

\u5982\u540c\u5fc3\u810f\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8eab\u4f53\u7684\u8840\u6db2\uff0c\u800c\u4e0d\u662f\u901a\u8fc7\u533b\u751f\u6765\u62bd\u8840\u3002<\/p>\n

\u7a0b\u5e8f\u7f16\u8bd1\u8fc7\u540e\u4f1a\u4ea7\u751f\u4e24\u4e2a .class \u6587\u4ef6\uff0c\u5206\u522b\u662f Out.class \u548c Out$In.class\u3002<\/p>\n

\u5176\u4e2d $ \u4ee3\u8868\u4e86\u4e0a\u9762\u7a0b\u5e8f\u4e2d Out.In \u4e2d\u7684\u90a3\u4e2a\u3002<\/p>\n

Out.In in = new Out().new In() \u53ef\u4ee5\u7528\u6765\u751f\u6210\u5185\u90e8\u7c7b\u7684\u5bf9\u8c61\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5b58\u5728\u4e24\u4e2a\u5c0f\u77e5\u8bc6\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n

    \n
  1. \u5f00\u5934\u7684 Out \u662f\u4e3a\u4e86\u6807\u660e\u9700\u8981\u751f\u6210\u7684\u5185\u90e8\u7c7b\u5bf9\u8c61\u5728\u54ea\u4e2a\u5916\u90e8\u7c7b\u5f53\u4e2d\u3002<\/li>\n
  2. \u5fc5\u987b\u5148\u6709\u5916\u90e8\u7c7b\u7684\u5bf9\u8c61\u624d\u80fd\u751f\u6210\u5185\u90e8\u7c7b\u7684\u5bf9\u8c61\uff0c\u56e0\u4e3a\u5185\u90e8\u7c7b\u7684\u4f5c\u7528\u5c31\u662f\u4e3a\u4e86\u8bbf\u95ee\u5916\u90e8\u7c7b\u4e2d\u7684\u6210\u5458\u53d8\u91cf\u3002<\/li>\n<\/ol>\n
    class Out {\r\n    private int age = 12;\r\n     \r\n    class In {\r\n        private int age = 13;\r\n        public void print() {\r\n            int age = 14;\r\n            System.out.println(\"\u5c40\u90e8\u53d8\u91cf\uff1a\" + age);\r\n            System.out.println(\"\u5185\u90e8\u7c7b\u53d8\u91cf\uff1a\" + this.age);\r\n            System.out.println(\"\u5916\u90e8\u7c7b\u53d8\u91cf\uff1a\" + Out.this.age);\r\n        }\r\n    }\r\n}\r\n \r\npublic class Demo {\r\n    public static void main(String[] args) {\r\n        Out.In in = new Out().new In();\r\n        in.print();\r\n    }\r\n}<\/pre>\n

    \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

    \u5c40\u90e8\u53d8\u91cf\uff1a14\r\n\u5185\u90e8\u7c7b\u53d8\u91cf\uff1a13\r\n\u5916\u90e8\u7c7b\u53d8\u91cf\uff1a12<\/pre>\n

    \u4ece\u5b9e\u4f8b1\u4e2d\u53ef\u4ee5\u53d1\u73b0\uff0c\u5185\u90e8\u7c7b\u5728\u6ca1\u6709\u540c\u540d\u6210\u5458\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u5185\u90e8\u7c7b\u4f1a\u76f4\u63a5\u8bbf\u95ee\u5916\u90e8\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff0c\u800c\u65e0\u9700\u6307\u5b9aOut.this.\u5c5e\u6027\u540d<\/strong>\u3002<\/p>\n

    \u5426\u5219\uff0c\u5185\u90e8\u7c7b\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u4f1a\u8986\u76d6\u5916\u90e8\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u3002<\/p>\n

    \u800c\u8bbf\u95ee\u5185\u90e8\u7c7b\u672c\u8eab\u7684\u6210\u5458\u53d8\u91cf\u53ef\u7528 this.\u5c5e\u6027\u540d<\/strong>\uff0c\u8bbf\u95ee\u5916\u90e8\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u9700\u8981\u4f7f\u7528 Out.this.\u5c5e\u6027\u540d<\/strong>\u3002<\/p>\n

    \u5b9e\u4f8b2\uff1a\u9759\u6001\u5185\u90e8\u7c7b<\/strong><\/div>\n
    class Out {\r\n    private static int age = 12;\r\n     \r\n    static class In {\r\n        public void print() {\r\n            System.out.println(age);\r\n        }\r\n    }\r\n}\r\n \r\npublic class Demo {\r\n    public static void main(String[] args) {\r\n        Out.In in = new Out.In();\r\n        in.print();\r\n    }\r\n}<\/pre>\n

    \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

    12<\/pre>\n

    \u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c\u7528 static \u5c06\u5185\u90e8\u5185\u9759\u6001\u5316\uff0c\u90a3\u4e48\u5185\u90e8\u7c7b\u5c31\u53ea\u80fd\u8bbf\u95ee\u5916\u90e8\u7c7b\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u5177\u6709\u5c40\u9650\u6027\u3002<\/p>\n

    \u5176\u6b21\uff0c\u56e0\u4e3a\u5185\u90e8\u7c7b\u88ab\u9759\u6001\u5316\uff0c\u56e0\u6b64 Out.In \u53ef\u4ee5\u5f53\u505a\u4e00\u4e2a\u6574\u4f53\u770b\uff0c\u53ef\u4ee5\u76f4\u63a5 new \u51fa\u5185\u90e8\u7c7b\u7684\u5bf9\u8c61\uff08\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee static\uff0c\u751f\u4e0d\u751f\u6210\u5916\u90e8\u7c7b\u5bf9\u8c61\u90fd\u6ca1\u5173\u7cfb\uff09<\/p>\n

    \u5b9e\u4f8b3\uff1a\u79c1\u6709\u5185\u90e8\u7c7b\/h3><\/strong><\/div>\n
    class Out {\r\n    private int age = 12;\r\n     \r\n    private class In {\r\n        public void print() {\r\n            System.out.println(age);\r\n        }\r\n    }\r\n    public void outPrint() {\r\n        new In().print();\r\n    }\r\n}\r\n \r\npublic class Demo {\r\n    public static void main(String[] args) {\r\n        \/\/\u6b64\u65b9\u6cd5\u65e0\u6548\r\n        \/*\r\n        Out.In in = new Out().new In();\r\n        in.print();\r\n        *\/\r\n        Out out = new Out();\r\n        out.outPrint();\r\n    }\r\n}<\/pre>\n

    \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

    12<\/pre>\n

    \u5982\u679c\u4e00\u4e2a\u5185\u90e8\u7c7b\u53ea\u5e0c\u671b\u88ab\u5916\u90e8\u7c7b\u4e2d\u7684\u65b9\u6cd5\u64cd\u4f5c\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 private \u58f0\u660e\u5185\u90e8\u7c7b\u3002<\/p>\n

    \u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u5728 Out \u7c7b\u91cc\u9762\u751f\u6210 In \u7c7b\u7684\u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\uff0c\u800c\u65e0\u6cd5\u518d\u4f7f\u7528 Out.In in = new Out().new In() \u751f\u6210\u5185\u90e8\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>\n

    \u4e5f\u5c31\u662f\u8bf4\uff0c\u6b64\u65f6\u7684\u5185\u90e8\u7c7b\u53ea\u6709\u5916\u90e8\u7c7b\u53ef\u63a7\u5236\u3002<\/p>\n

    \u5982\u540c\u662f\uff0c\u6211\u7684\u5fc3\u810f\u53ea\u80fd\u7531\u6211\u7684\u8eab\u4f53\u63a7\u5236\uff0c\u5176\u4ed6\u4eba\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5b83\u3002<\/p>\n

    \u5b9e\u4f8b4\uff1a\u65b9\u6cd5\u5185\u90e8\u7c7b<\/strong><\/div>\n
    class Out {\r\n    private int age = 12;\r\n \r\n    public void Print(final int x) {\r\n        class In {\r\n            public void inPrint() {\r\n                System.out.println(x);\r\n                System.out.println(age);\r\n            }\r\n        }\r\n        new In().inPrint();\r\n    }\r\n}\r\n \r\npublic class Demo {\r\n    public static void main(String[] args) {\r\n        Out out = new Out();\r\n        out.Print(3);\r\n    }\r\n}<\/pre>\n

    \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

    3\r\n12<\/pre>\n

    \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u5185\u90e8\u7c7b\u79fb\u5230\u4e86\u5916\u90e8\u7c7b\u7684\u65b9\u6cd5\u4e2d\uff0c\u7136\u540e\u5728\u5916\u90e8\u7c7b\u7684\u65b9\u6cd5\u4e2d\u518d\u751f\u6210\u4e00\u4e2a\u5185\u90e8\u7c7b\u5bf9\u8c61\u53bb\u8c03\u7528\u5185\u90e8\u7c7b\u65b9\u6cd5\u3002<\/p>\n

    \u5982\u679c\u6b64\u65f6\u6211\u4eec\u9700\u8981\u5f80\u5916\u90e8\u7c7b\u7684\u65b9\u6cd5\u4e2d\u4f20\u5165\u53c2\u6570\uff0c\u90a3\u4e48\u5916\u90e8\u7c7b\u7684\u65b9\u6cd5\u5f62\u53c2\u5fc5\u987b\u4f7f\u7528 final \u5b9a\u4e49\u3002<\/p>\n

    \u81f3\u4e8e final \u5728\u8fd9\u91cc\u5e76\u6ca1\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u53ea\u662f\u4e00\u79cd\u8868\u793a\u5f62\u5f0f\u800c\u5df2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u663e\u7136\uff0c\u6b64\u5904\u4e0d\u80fd\u5355\u65b9\u9762\u7528\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\u8868\u793a\u4e00\u4e2a\u5fc3\u810f\uff0c\u800c\u9700\u8981\u4e00\u4e2a\u7c7b\u3002 \u800c\u5fc3\u810f\u53c8\u5728\u4eba\u4f53\u5f53\u4e2d\uff0c\u6b63\u5982\u540c\u662f\u5185\u90e8\u7c7b\u5728\u5916\u90e8\u5185\u5f53\u4e2d […]<\/p>\n","protected":false},"author":1470,"featured_media":221390,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-221388","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=221388"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221388\/revisions"}],"predecessor-version":[{"id":221392,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221388\/revisions\/221392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/221390"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=221388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=221388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=221388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}