{"id":203318,"date":"2025-05-22T11:28:11","date_gmt":"2025-05-22T03:28:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203318\/"},"modified":"2025-05-22T11:28:11","modified_gmt":"2025-05-22T03:28:11","slug":"%e6%8a%bd%e8%b1%a1%ef%bc%9a%e8%a7%a3%e7%a0%81-java-%e4%b8%ad%e7%9a%84%e6%8a%bd%e8%b1%a1%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203318\/","title":{"rendered":"\u62bd\u8c61\uff1a\u89e3\u7801 Java \u4e2d\u7684\u62bd\u8c61\u7c7b"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u62bd\u8c61\uff1a\u89e3\u7801 Java \u4e2d\u7684\u62bd\u8c61\u7c7b<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u62bd\u8c61\uff1a\u89e3\u7801 Java \u4e2d\u7684\u62bd\u8c61\u7c7b\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241104\/17307227286728bba8d0891.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8<strong>\u62bd\u8c61\u7c7b<\/strong>\uff0c\u5b83\u662f java \u4e2d\u62bd\u8c61\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u6211\u4eec\u5c06\u5efa\u7acb\u5728\u524d\u9762\u8ba8\u8bba\u7684\u6982\u5ff5\u7684\u57fa\u7840\u4e0a\uff0c\u5e76\u7814\u7a76\u62bd\u8c61\u5982\u4f55\u7b80\u5316\u590d\u6742\u7684\u7cfb\u7edf\u3002\u62bd\u8c61\u7c7b\u5145\u5f53\u5176\u4ed6\u7c7b\u7684\u84dd\u56fe\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u53ea\u5173\u6ce8\u76f8\u5173\u7ec6\u8282\uff0c\u540c\u65f6\u9690\u85cf\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\u3002 <\/p>\n<p>\u8ba9\u6211\u4eec\u66f4\u6df1\u5165\u5730\u4e86\u89e3<strong>\u4ec0\u4e48\u662f\u62bd\u8c61\u7c7b\u3001\u5b83\u4eec\u4e3a\u4f55\u5b58\u5728\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4eec<\/strong>\u3002 <\/p>\n<hr>\n<h2> \u4ec0\u4e48\u662f\u62bd\u8c61\u7c7b\uff1f <\/h2>\n<p><strong>\u62bd\u8c61\u7c7b<\/strong>\u662f\u4e00\u4e2a\u4e0d\u80fd\u5355\u72ec\u5b9e\u4f8b\u5316\u7684\u7c7b\u3002\u5b83\u65e8\u5728\u901a\u8fc7\u4e3a\u5176\u62bd\u8c61\u65b9\u6cd5\u63d0\u4f9b\u5177\u4f53\u5b9e\u73b0\u7684\u5b50\u7c7b\u8fdb\u884c\u6269\u5c55\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u62bd\u8c61\u7c7b<strong>\u4e3a\u5176\u5b50\u7c7b\u5960\u5b9a\u4e86\u57fa\u7840<\/strong>\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u4ee3\u7801\u91cd\u7528\u548c\u5f3a\u5236\u7ed3\u6784\u3002 <\/p>\n<h3> \u62bd\u8c61\u7c7b\u7684\u4e3b\u8981\u7279\u70b9\uff1a <\/h3>\n<ul>\n<li>\n<p>\u4f7f\u7528abstract\u5173\u952e\u5b57\u5b9a\u4e49\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ef\u4ee5\u5305\u542b\u5fc5\u987b\u7531\u5b50\u7c7b\u5b9e\u73b0\u7684<strong>\u62bd\u8c61\u65b9\u6cd5<\/strong>\uff08\u6ca1\u6709\u4e3b\u4f53\u7684\u65b9\u6cd5\uff09\u3002 <\/p>\n<\/li>\n<li>\n<p>\u8fd8\u53ef\u4ee5\u6709\u5177\u6709\u5b8c\u6574\u5b9e\u73b0\u7684<strong>\u5177\u4f53\u65b9\u6cd5<\/strong>\u3002 <\/p>\n<\/li>\n<li>\n<p>\u53ef\u4ee5\u58f0\u660e<strong>\u6784\u9020\u51fd\u6570\u3001\u5b57\u6bb5\u548c\u975e\u62bd\u8c61\u65b9\u6cd5<\/strong>\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u65e0\u6cd5\u76f4\u63a5\u5b9e\u4f8b\u5316<\/strong>\u3002<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h2> \u4e3a\u4ec0\u4e48\u9700\u8981\u62bd\u8c61\u7c7b\uff1f <\/h2>\n<p>\u62bd\u8c61\u7c7b\u5141\u8bb8\u5f00\u53d1\u4eba\u5458<strong>\u5411\u7528\u6237\u9690\u85cf\u4e0d\u5fc5\u8981\u7684\u5b9e\u73b0\u7ec6\u8282<\/strong>\uff0c\u540c\u65f6\u786e\u4fdd\u6240\u6709\u5b50\u7c7b\u90fd\u5b9e\u73b0\u67d0\u4e9b\u65b9\u6cd5\u3002\u5b83\u4eec\u5145\u5f53<strong>\u534a\u5b9a\u4e49\u6a21\u677f<\/strong>\uff0c\u5e76\u901a\u8fc7\u5728\u57fa\u7c7b\u4e2d\u63d0\u4f9b\u5171\u4eab\u529f\u80fd\u6765\u9f13\u52b1\u4ee3\u7801\u91cd\u7528\u3002 <\/p>\n<hr>\n<h2> \u793a\u4f8b\uff1a\u5458\u5de5\u548c\u7ecf\u7406 <\/h2>\n<p>\u4e3a\u4e86\u6f14\u793a\u62bd\u8c61\u7c7b\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u4ee5\u4e0b\u6d89\u53ca\u62bd\u8c61 <strong>employeeparent<\/strong> \u7c7b\u53ca\u5176 <strong>managerchild<\/strong> \u5b50\u7c7b\u7684\u793a\u4f8b\u3002\u7236\u7c7b\u62e5\u6709\u5171\u4eab\u529f\u80fd\uff0c\u800c\u5b50\u7c7b\u5219\u5b8c\u6210\u5177\u4f53\u7ec6\u8282\uff08\u5982\u7ee9\u6548\u5956\u91d1\uff09\u7684\u5b9e\u73b0\u3002<\/p>\n<h3> employeeparent.java <\/h3>\n<pre>package oops.abstract_class;\n\npublic abstract class employeeparent {\n    private int id, depid;\n    private string name;\n    private double basicsal;\n\n    public employeeparent(int id, string name, int deptid, double basicsal) {\n        this.id = id;\n        this.depid = deptid;\n        this.name = name;\n        this.basicsal = basicsal;\n    }\n\n    \/\/ abstract method to be implemented by subclasses.\n    protected abstract double computenetsalary();\n\n    protected double getbasicsal() {\n        return basicsal;\n    }\n\n    @override\n    public string tostring() {\n        return \"employeeparent [id=\" + id + \", depid=\" + depid +\n               \", name=\" + name + \", basicsal=\" + basicsal + \"]\";\n    }\n}\n<\/pre>\n<hr>\n<h3> managerchild.java <\/h3>\n<pre>package oops.abstract_class;\n\npublic class ManagerChild extends EmployeeParent {\n    private double perfBonus;\n\n    public ManagerChild(int id, String name, int deptId, double basicSal, double perfBonus) {\n        \/\/ Calling the constructor of the abstract class.\n        super(id, name, deptId, basicSal);\n        this.perfBonus = perfBonus;\n    }\n\n    \/\/ Implementing the abstract method from EmployeeParent.\n    @Override\n    public double computeNetSalary() {\n        return getBasicSal() + perfBonus;\n    }\n\n    @Override\n    public String toString() {\n        return \"ManagerChild [perfBonus=\" + perfBonus + \"\\n\" + super.toString() + \"]\";\n    }\n\n    public static void main(String[] args) {\n        ManagerChild manager = new ManagerChild(1, \"Arshi\", 2, 10000, 1890);\n        System.out.println(manager);\n        System.out.println(\"Net Salary: \" + manager.computeNetSalary());\n\n        \/\/ Abstract class cannot be instantiated \n        EmployeeParent employee = new EmployeeParent(); \/\/ Compile Error\n    }\n}\n<\/pre>\n<hr>\n<h2> \u5206\u89e3\u793a\u4f8b <\/h2>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff1a<\/p>\n<ol>\n<li>\n<p><strong>employeeparent<\/strong> \u662f\u5b9a\u4e49\u5176\u5b50\u7c7b\u7ed3\u6784\u7684\u62bd\u8c61\u7c7b\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b83\u5305\u542b<strong>\u5177\u4f53\u65b9\u6cd5<\/strong>\uff08\u5982tostring\u548cgetbasicsal\uff09\u548c<strong>\u62bd\u8c61\u65b9\u6cd5<\/strong>\uff08computenetsalary\uff09\uff0c\u5b83\u5fc5\u987b\u7531\u4efb\u4f55\u5b50\u7c7b\u5b9e\u73b0\u3002<\/p>\n<\/li>\n<li>\n<p><strong>managerchild<\/strong> \u6269\u5c55\u4e86 <strong>employeeparent<\/strong> \u5e76\u63d0\u4f9b\u4e86\u8ba1\u7b97\u51c0\u5de5\u8d44\u7684\u5177\u4f53\u903b\u8f91\uff0c\u5305\u62ec\u7ee9\u6548\u5956\u91d1\u3002<\/p>\n<\/li>\n<li>\n<p>\u62bd\u8c61\u7c7b\u6784\u9020\u51fd\u6570\u662f\u901a\u8fc7\u5b50\u7c7b\u6784\u9020\u51fd\u6570\u4e2d\u7684 super() \u8c03\u7528\u6765\u8c03\u7528\u7684\uff0c\u56e0\u4e3a\u62bd\u8c61\u7c7b\u4e0d\u80fd\u76f4\u63a5\u5b9e\u4f8b\u5316\u3002<\/p>\n<\/li>\n<\/ol>\n<hr>\n<h2> \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u62bd\u8c61\u7c7b\uff1f <\/h2>\n<ul>\n<li>\n<p><strong>\u4ee3\u7801\u53ef\u91cd\u7528\u6027\uff1a<\/strong>\u5f53\u591a\u4e2a\u7c7b\u5171\u4eab\u516c\u5171\u5b57\u6bb5\u548c\u884c\u4e3a\u65f6\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5f3a\u5236\u7ed3\u6784\uff1a<\/strong>\u5f53\u60a8\u60f3\u8981\u5f3a\u5236\u5728\u5b50\u7c7b\u4e2d\u5b9e\u73b0\u67d0\u4e9b\u65b9\u6cd5\u65f6\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u90e8\u5206\u5b9e\u73b0\uff1a<\/strong>\u5f53\u67d0\u4e9b\u903b\u8f91\u53ef\u4ee5\u5728\u7c7b\u4e4b\u95f4\u5171\u4eab\uff0c\u4f46\u5177\u4f53\u903b\u8f91\u6709\u6240\u4e0d\u540c\u65f6\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u7528\u4f8b\u793a\u4f8b\uff1a<\/strong> \u60a8\u53ef\u4ee5\u62e5\u6709\u4e00\u4e2a\u5177\u6709\u516c\u5171\u5c5e\u6027\uff08\u5982\u989c\u8272\uff09\u548c\u62bd\u8c61\u65b9\u6cd5\uff08\u5982 getarea\uff09\u7684\u62bd\u8c61 shape \u7c7b\uff0c\u4e0d\u540c\u7684\u5f62\u72b6\uff08\u5706\u5f62\u3001\u77e9\u5f62\uff09\u4f1a\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002 <\/p>\n<\/li>\n<\/ul>\n<hr>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u62bd\u8c61\u7c7b\u5728 java \u4e2d\u53d1\u6325\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\uff0c<strong>\u5728\u5b8c\u5168\u62bd\u8c61\u548c\u5177\u4f53\u5b9e\u73b0\u4e4b\u95f4\u63d0\u4f9b\u5e73\u8861<\/strong>\u3002\u5b83\u4eec\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f<strong>\u4e13\u6ce8\u4e8e\u5fc5\u8981\u7684\u4e8b\u60c5\uff0c\u540c\u65f6\u5411\u7528\u6237\u9690\u85cf\u590d\u6742\u7684\u7ec6\u8282<\/strong>\u3002\u7406\u89e3\u5e76\u6709\u6548\u5730\u4f7f\u7528\u62bd\u8c61\u7c7b\u662f\u638c\u63e1\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u4e00\u6b65\u3002 <\/p>\n<p>\u8bf7\u7ee7\u7eed\u5173\u6ce8\u672c\u7cfb\u5217\u7684\u4e0b\u4e00\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8<strong>\u63a5\u53e3<\/strong>\u4ee5\u53ca\u5b83\u4eec\u4e0e\u62bd\u8c61\u7c7b\u6709\u4f55\u4e0d\u540c\uff01<\/p>\n<hr>\n<h2> \u76f8\u5173\u5e16\u5b50 <\/h2>\n<ul>\n<li>\n<p>java \u57fa\u7840<\/p>\n<\/li>\n<li>\n<p>\u6570\u7ec4\u9762\u8bd5\u8981\u70b9<\/p>\n<\/li>\n<li>\n<p>java \u5185\u5b58\u57fa\u7840<\/p>\n<\/li>\n<li>\n<p>java \u5173\u952e\u5b57\u8981\u70b9<\/p>\n<\/li>\n<li>\n<p>\u96c6\u5408\u6846\u67b6\u8981\u70b9<\/p>\n<\/li>\n<\/ul>\n<p>\u7f16\u7801\u5feb\u4e50\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u62bd\u8c61\uff1a\u89e3\u7801 Java \u4e2d\u7684\u62bd\u8c61\u7c7b\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u62bd\u8c61\uff1a\u89e3\u7801 Java \u4e2d\u7684\u62bd\u8c61\u7c7b&#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-203318","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203318","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=203318"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203318\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}