{"id":202561,"date":"2025-05-22T10:26:44","date_gmt":"2025-05-22T02:26:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202561\/"},"modified":"2025-05-22T10:26:44","modified_gmt":"2025-05-22T02:26:44","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8java%e4%b8%ad%e5%ae%9a%e4%b9%89%e5%8f%97%e4%bf%9d%e6%8a%a4%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202561\/","title":{"rendered":"\u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49\u53d7\u4fdd\u62a4\u7684\u51fd\u6570"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49\u53d7\u4fdd\u62a4\u7684\u51fd\u6570<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49\u53d7\u4fdd\u62a4\u7684\u51fd\u6570\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>\u53d7\u4fdd\u62a4\u7684\u51fd\u6570\u5728 Java \u4e2d\u53ef\u4ee5\u4f7f\u7528 protected \u4fee\u9970\u7b26\u5b9a\u4e49\uff0c\u5141\u8bb8\u5b50\u7c7b\u548c\u540c\u4e00\u4e2a\u5305\u4e2d\u7684\u7c7b\u8bbf\u95ee\u3002\u5b83\u6bd4\u5305\u53ef\u89c1\u6027\u66f4\u5177\u53ef\u89c1\u6027\uff0c\u4f46\u9650\u5236\u4e86\u4e0d\u540c\u5305\u4e2d\u7c7b\u7684\u8bbf\u95ee\u3002\u8bed\u6cd5\u4e3a\uff1aprotected \u4fee\u9970\u7b26\u540d\u79f0(\u53c2\u6570\u5217\u8868) { \/\/ \u51fd\u6570\u4f53}\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729917999671c742fd6ab2.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u5b9a\u4e49\u53d7\u4fdd\u62a4\u7684\u51fd\u6570<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53d7\u4fdd\u62a4\uff08protected\uff09\u8bbf\u95ee\u63a7\u5236\u6743\u9650\u4fee\u9970\u7b26\u7528\u4e8e\u9650\u5236\u65b9\u6cd5\u6216\u5b57\u6bb5\u7684\u53ef\u89c1\u6027\uff0c\u4f7f\u5176\u4ec5\u5bf9\u5b50\u7c7b\u548c\u540c\u4e00\u4e2a\u5305\u4e2d\u7684\u5176\u4ed6\u7c7b\u53ef\u89c1\u3002\u8fd9\u63d0\u4f9b\u4e86\u6bd4\u9ed8\u8ba4\u5305\u53ef\u89c1\u6027\u66f4\u9ad8\u7684\u53ef\u89c1\u6027\uff0c\u540c\u65f6\u53c8\u963b\u6b62\u4e86\u4e0d\u540c\u5305\u4e2d\u7684\u7c7b\u8bbf\u95ee\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>protected \u4fee\u9970\u7b26\u540d\u79f0(\u53c2\u6570\u5217\u8868) {\n  \/\/ \u51fd\u6570\u4f53\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n<pre>class Parent {\n    protected void printParent() {\n        System.out.println(\"\u6211\u662f Parent \u7c7b\u3002\");\n    }\n}\n\nclass Child extends Parent {\n    public void printChild() {\n        printParent();  \/\/ \u53ef\u4ee5\u8bbf\u95ee protected \u65b9\u6cd5\n        System.out.println(\"\u6211\u662f Child \u7c7b\u3002\");\n    }\n}\n\nclass OtherClass {\n    public void printOther() {\n        \/\/ \u65e0\u6cd5\u8bbf\u95ee protected \u65b9\u6cd5\n        \/\/ printParent();  \/\/ \u7f16\u8bd1\u9519\u8bef\n        System.out.println(\"\u6211\u662f OtherClass \u7c7b\u3002\");\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        Parent parent = new Parent();\n        \/\/ \u65e0\u6cd5\u8bbf\u95ee protected \u65b9\u6cd5\n        \/\/ parent.printParent();  \/\/ \u7f16\u8bd1\u9519\u8bef\n\n        Child child = new Child();\n        child.printChild();\n\n        OtherClass other = new OtherClass();\n        other.printOther();\n    }\n}<\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<pre>\u6211\u662f Parent \u7c7b\u3002\n\u6211\u662f Child \u7c7b\u3002\n\u6211\u662f OtherClass \u7c7b\u3002<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n<ul>\n<li><code>printParent()<\/code> \u65b9\u6cd5\u662f\u53d7\u4fdd\u62a4\u7684\uff0c\u53ea\u80fd\u88ab <code>Parent<\/code> \u7c7b\u53ca\u5176\u5b50\u7c7b (<code>Child<\/code>) \u8bbf\u95ee\u3002<\/li>\n<li>\u5176\u4ed6\u7c7b\uff08\u4f8b\u5982 <code>OtherClass<\/code>\uff09\u65e0\u6cd5\u8bbf\u95ee\u53d7\u4fdd\u62a4\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49\u53d7\u4fdd\u62a4\u7684\u51fd\u6570\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49\u53d7\u4fdd\u62a4\u7684\u51fd\u6570&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202561","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202561"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202561\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}