{"id":703,"date":"2022-10-05T23:27:50","date_gmt":"2022-10-05T15:27:50","guid":{"rendered":"https:\/\/qaqaq.top\/?p=703"},"modified":"2022-12-19T20:06:51","modified_gmt":"2022-12-19T12:06:51","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%ad-super%e8%b0%83%e7%94%a8%e5%b1%9e%e6%80%a7%e5%92%8c%e6%96%b9%e6%b3%95%e3%80%81super%e8%b0%83%e7%94%a8%e6%9e%84%e9%80%a0%e5%99%a8","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=703","title":{"rendered":"\u9762\u5411\u5bf9\u8c61(\u4e2d)-super\u8c03\u7528\u5c5e\u6027\u548c\u65b9\u6cd5\u3001super\u8c03\u7528\u6784\u9020\u5668"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P276;\n\npublic class Person {\n\n\tString name;\n\tint age;\n\tint id = 1001;\/\/\u8eab\u4efd\u8bc1\u53f7\n\t\n\tpublic Person() {\n\t\tSystem.out.println(\"\u6211\u65e0\u5904\u4e0d\u5728\uff01\");\n\t}\n\t\n\tpublic Person(String name) {\n\t\tthis.name = name;\n\t}\n\t\n\tpublic Person(String name, int age) {\n\t\tthis(name);\n\t\tthis.age = age;\n\t}\n\t\n\tpublic void eat() {\n\t\tSystem.out.println(\"\u4eba\uff1a\u5403\u996d\");\n\t}\n\t\n\tpublic void walk() {\n\t\tSystem.out.println(\"\u4eba\uff1a\u8d70\u8def\");\n\t}\n\t\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P276;\n\npublic class Student extends Person {\n\n\tString major;\n\tint id = 1002;\/\/\u5b66\u53f7\n\t\n\tpublic Student() {\n\t\tsuper();\n\t}\n\t\n\tpublic Student(String major) {\n\t\tsuper();\n\t\tthis.major = major;\n\t}\n\t\n\tpublic Student(String name, int age, String major) {\n\/\/\t\tthis.name = name;\n\/\/\t\tthis.age = age;\n\t\tsuper(name,age);\n\t\tthis.major = major;\n\t}\n\t\n\t@Override\n\tpublic void eat() {\n\t\tSystem.out.println(\"\u5b66\u751f\uff1a\u591a\u5403\u6709\u8425\u517b\u7684\u98df\u7269\");\n\t}\n\t\n\tpublic void stduy() {\n\t\tSystem.out.println(\"\u5b66\u751f\uff1a\u5b66\u4e60\u77e5\u8bc6\");\n\t\tthis.eat();\n\t\tsuper.eat();\n\t\twalk();\n\t}\n\t\n\tpublic void show() {\n\t\tSystem.out.println(\"name = \" + name + \",age = \" + age);\n\t\tSystem.out.println(\"id = \" + this.id);\n\t\tSystem.out.println(\"id = \" + super.id);\n\t}\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P276;\n\/*\n * super\u5173\u952e\u5b57\u7684\u4f7f\u7528\n * 1. super\u7406\u89e3\u4e3a\uff1a\u7236\u7c7b\u7684\n * 2. super\u53ef\u4ee5\u7528\u6765\u8c03\u7528\uff1a\u5c5e\u6027\u3001\u65b9\u6cd5\u3001\u6784\u9020\u5668\n * \n * 3. super\u7684\u4f7f\u7528\uff1a\u8c03\u7528\u5c5e\u6027\u548c\u65b9\u6cd5\n * \n * \t\t3.1 \u6211\u4eec\u53ef\u4ee5\u5728\u5b50\u7c7b\u7684\u65b9\u6cd5\u6216\u6784\u9020\u5668\u4e2d\u3002\u901a\u8fc7\u4f7f\u7528\"super.\u5c5e\u6027\"\u6216\"super.\u65b9\u6cd5\"\u7684\u65b9\u5f0f\uff0c\u663e\u5f0f\u7684\u8c03\u7528\n * \t\t\u7236\u7c7b\u4e2d\u58f0\u660e\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e60\u60ef\u7701\u7565\"super.\"\n * \t\t3.2 \u7279\u6b8a\u60c5\u51b5\uff1a\u5f53\u5b50\u7c7b\u548c\u7236\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u540c\u540d\u7684\u5c5e\u6027\u65f6\uff0c\u6211\u4eec\u8981\u60f3\u5728\u5b50\u7c7b\u4e2d\u8c03\u7528\u7236\u7c7b\u4e2d\u58f0\u660e\u7684\u5c5e\u6027\uff0c\u5219\u5fc5\u987b\u663e\u5f0f\u7684\n * \t\t\u4f7f\u7528\"super.\u5c5e\u6027\"\u7684\u65b9\u5f0f\uff0c\u8868\u660e\u8c03\u7528\u7684\u662f\u7236\u7c7b\u4e2d\u58f0\u660e\u7684\u5c5e\u6027\u3002\n * \t\t3.3 \u7279\u6b8a\u60c5\u51b5\uff1a\u5f53\u5b50\u7c7b\u91cd\u5199\u4e86\u7236\u7c7b\u4e2d\u7684\u65b9\u6cd5\u4ee5\u540e\uff0c\u6211\u4eec\u60f3\u5728\u5b50\u7c7b\u7684\u65b9\u6cd5\u4e2d\u8c03\u7528\u7236\u7c7b\u4e2d\u88ab\u91cd\u5199\u7684\u65b9\u6cd5\u65f6\uff0c\u5219\u5fc5\u987b\u663e\u5f0f\u7684\n * \t\t\u4f7f\u7528\"super.\u65b9\u6cd5\"\u7684\u65b9\u5f0f\uff0c\u8868\u660e\u8c03\u7528\u7684\u662f\u7236\u7c7b\u4e2d\u88ab\u91cd\u5199\u7684\u65b9\u6cd5\u3002\n * \n * 4. super\u8c03\u7528\u6784\u9020\u5668\n * \t\t4.1 \u6211\u4eec\u53ef\u4ee5\u5728\u5b50\u7c7b\u7684\u6784\u9020\u5668\u4e2d\u663e\u5f0f\u7684\u4f7f\u7528\"super(\u5f62\u53c2\u5217\u8868)\"\u7684\u65b9\u5f0f\u3002\u8c03\u7528\u7236\u7c7b\u4e2d\u58f0\u660e\u7684\u6307\u5b9a\u7684\u6784\u9020\u5668\n * \t\t4.2 \"super(\u5f62\u53c2\u5217\u8868)\"\u7684\u4f7f\u7528\uff0c\u5fc5\u987b\u58f0\u660e\u5728\u5b50\u7c7b\u6784\u9020\u5668\u7684\u9996\u884c\uff01\n * \t\t4.3 \u6211\u4eec\u5728\u7c7b\u7684\u6784\u9020\u5668\u4e2d\uff0c\u9488\u5bf9\u4e8e\"this(\u5f62\u53c2\u5217\u8868)\"\u6216\"super(\u5f62\u53c2\u5217\u8868)\"\u53ea\u80fd\u4e8c\u9009\u4e00\uff0c\u4e0d\u80fd\u540c\u65f6\u51fa\u73b0\n * \t\t4.4 \u5728\u6784\u9020\u5668\u7684\u9996\u884c\uff0c\u6ca1\u6709\u663e\u5f0f\u7684\u58f0\u660e\"this(\u5f62\u53c2\u5217\u8868)\"\u6216\"super(\u5f62\u53c2\u5217\u8868)\"\uff0c\u5219\u9ed8\u8ba4\u8c03\u7528\u7684\u662f\u7236\u7c7b\u4e2d\u7a7a\u53c2\u7684\u6784\u9020\u5668\n * \t\t4.5 \u5728\u7c7b\u7684\u591a\u4e2a\u6784\u9020\u5668\u4e2d\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u7c7b\u7684\u6784\u9020\u5668\u4e2d\u4f7f\u7528\u4e86super(\u5f62\u53c2\u5217\u8868)\uff0c\u8c03\u7528\u7236\u7c7b\u4e2d\u7684\u6784\u9020\u5668\n * \n *\/\npublic class SuperTest {\n\t\n\tpublic static void main(String&#91;] args) {\n\t\t\n\t\tStudent s = new Student();\n\t\ts.show();\n\t\t\n\t\tSystem.out.println();\n\t\t\n\t\ts.stduy();\n\t\t\n\t\tStudent s1 = new Student(\"Tom\", 21, \"IT\");\n\t\ts1.show();\n\t\t\n\t\tSystem.out.println(\"**********************\");\n\t\tStudent s2 = new Student();\n\t}\n\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P276;\n\npublic class Circle {\n\n\tprivate double radius;\/\/ \u534a\u5f84\n\n\tpublic Circle() {\n\t\tradius = 1.0;\n\t}\n\t\n\tpublic Circle(double radius) {\n\t\tthis.radius = radius;\n\t}\n\n\tpublic double getRadius() {\n\t\treturn radius;\n\t}\n\n\tpublic void setRadius(double radius) {\n\t\tthis.radius = radius;\n\t}\n\n\t\/\/\u8fd4\u56de\u5706\u7684\u9762\u79ef\n\tpublic double findArea() {\n\t\treturn Math.PI * radius * radius;\n\t}\n\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P276;\n\npublic class Cylinder extends Circle {\n\n\tprivate double length;\/\/ \u9ad8\n\n\tpublic Cylinder() {\n\t\tlength = 1.0;\n\t}\n\n\tpublic double getLength() {\n\t\treturn length;\n\t}\n\n\tpublic void setLength(double length) {\n\t\tthis.length = length;\n\t}\n\n\t\/\/\u8fd4\u56de\u5706\u67f1\u7684\u4f53\u79ef\n\tpublic double findVolume() {\n\/\/\t\treturn Math.PI * getRadius() * getRadius() * getLength();\n\t\treturn super.findArea() * getLength();\n\t}\n\t\n\t@Override\n\tpublic double findArea() {\/\/\u8fd4\u56de\u5706\u67f1\u7684\u8868\u9762\u79ef\n\t\treturn Math.PI * getRadius() * getRadius() * 2 + \n\t\t\t\t2 * Math.PI * getRadius() * getLength();\n\t}\n\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P276;\n\npublic class CylinderTest {\n\n\tpublic static void main(String&#91;] args) {\n\t\t\n\t\tCylinder cy = new Cylinder();\n\t\t\n\t\tcy.setRadius(2.1);\n\t\tcy.setLength(3.4);\n\t\tdouble volume = cy.findVolume();\n\t\tSystem.out.println(\"\u5706\u67f1\u7684\u4f53\u79ef\u4e3a\uff1a\" + volume);\n\t\t\n\t\t\/\/\u6ca1\u6709\u91cd\u5199findArea()\u65f6;\n\/\/\t\tdouble area = cy.findArea();\n\/\/\t\tSystem.out.println(\"\u5e95\u9762\u5706\u7684\u9762\u79ef\uff1a\" + area);\n\t\t\/\/\u91cd\u5199findArea()\u4ee5\u540e\n\t\tdouble area = cy.findArea();\n\t\tSystem.out.println(\"\u5706\u67f1\u7684\u8868\u9762\u79ef\uff1a\" + area);\n\t\t\n\t\tSystem.out.println(\"*************************\");\n\t\tCylinder cy1 = new Cylinder();\n\t\tdouble volume1 = cy1.findVolume();\n\t\tSystem.out.println(\"\u5706\u67f1\u7684\u4f53\u79ef\u4e3a\uff1a\" + volume1);\n\t}\n}\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-703","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\/703"}],"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=703"}],"version-history":[{"count":2,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/703\/revisions"}],"predecessor-version":[{"id":2655,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/703\/revisions\/2655"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}