{"id":216860,"date":"2021-05-02T09:21:32","date_gmt":"2021-05-02T01:21:32","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=216860"},"modified":"2021-04-25T09:22:35","modified_gmt":"2021-04-25T01:22:35","slug":"overview-and-similarities","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/overview-and-similarities.html","title":{"rendered":"Java \u4e2d this \u548c super \u7684\u7528\u6cd5\u6982\u8ff0\u53ca\u5f02\u540c"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nthis \u662f\u81ea\u8eab\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ee3\u8868\u5bf9\u8c61\u672c\u8eab\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a\u6307\u5411\u5bf9\u8c61\u672c\u8eab\u7684\u4e00\u4e2a\u6307\u9488\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

this<\/strong><\/div>\n

this \u7684\u7528\u6cd5\u5728 Java \u4e2d\u5927\u4f53\u53ef\u4ee5\u5206\u4e3a3\u79cd\uff1a<\/p>\n

1.\u666e\u901a\u7684\u76f4\u63a5\u5f15\u7528<\/strong><\/p>\n

\u8fd9\u79cd\u5c31\u4e0d\u7528\u8bb2\u4e86\uff0cthis \u76f8\u5f53\u4e8e\u662f\u6307\u5411\u5f53\u524d\u5bf9\u8c61\u672c\u8eab\u3002<\/p>\n

2.\u5f62\u53c2\u4e0e\u6210\u5458\u540d\u5b57\u91cd\u540d\uff0c\u7528 this \u6765\u533a\u5206\uff1a<\/strong><\/p>\n

\u5b9e\u4f8b<\/strong><\/p>\n

class Person {\r\n    private int age = 10;\r\n    public Person(){\r\n    System.out.println(\"\u521d\u59cb\u5316\u5e74\u9f84\uff1a\"+age);\r\n}\r\n    public int GetAge(int age){\r\n        this.age = age;\r\n        return this.age;\r\n    }\r\n}\r\n \r\npublic class test1 {\r\n    public static void main(String[] args) {\r\n        Person Harry = new Person();\r\n        System.out.println(\"Harry's age is \"+Harry.GetAge(12));\r\n    }\r\n}<\/pre>\n

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

\u521d\u59cb\u5316\u5e74\u9f84\uff1a10\r\nHarry's age is 12<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u91cc age \u662f GetAge \u6210\u5458\u65b9\u6cd5\u7684\u5f62\u53c2\uff0cthis.age \u662f Person \u7c7b\u7684\u6210\u5458\u53d8\u91cf\u3002<\/p>\n

3.\u5f15\u7528\u6784\u9020\u51fd\u6570<\/strong><\/p>\n

\u8fd9\u4e2a\u548c super \u653e\u5728\u4e00\u8d77\u8bb2\uff0c\u89c1\u4e0b\u9762\u3002<\/p>\n

super<\/strong><\/div>\n

super \u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u6307\u5411\u81ea\u5df1\u8d85\uff08\u7236\uff09\u7c7b\u5bf9\u8c61\u7684\u4e00\u4e2a\u6307\u9488\uff0c\u800c\u8fd9\u4e2a\u8d85\u7c7b\u6307\u7684\u662f\u79bb\u81ea\u5df1\u6700\u8fd1\u7684\u4e00\u4e2a\u7236\u7c7b\u3002
\nsuper \u4e5f\u6709\u4e09\u79cd\u7528\u6cd5\uff1a<\/p>\n

1.\u666e\u901a\u7684\u76f4\u63a5\u5f15\u7528<\/strong><\/p>\n

\u4e0e this \u7c7b\u4f3c\uff0csuper \u76f8\u5f53\u4e8e\u662f\u6307\u5411\u5f53\u524d\u5bf9\u8c61\u7684\u7236\u7c7b\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u7528 super.xxx \u6765\u5f15\u7528\u7236\u7c7b\u7684\u6210\u5458\u3002<\/p>\n

2.\u5b50\u7c7b\u4e2d\u7684\u6210\u5458\u53d8\u91cf\u6216\u65b9\u6cd5\u4e0e\u7236\u7c7b\u4e2d\u7684\u6210\u5458\u53d8\u91cf\u6216\u65b9\u6cd5\u540c\u540d<\/strong><\/p>\n

\u5b9e\u4f8b<\/strong><\/p>\n

class Country {\r\n    String name;\r\n    void value() {\r\n       name = \"China\";\r\n    }\r\n}\r\n  \r\nclass City extends Country {\r\n    String name;\r\n    void value() {\r\n    name = \"Shanghai\";\r\n    super.value();      \/\/\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\r\n    System.out.println(name);\r\n    System.out.println(super.name);\r\n    }\r\n  \r\n    public static void main(String[] args) {\r\n       City c=new City();\r\n       c.value();\r\n       }\r\n}<\/pre>\n

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

Shanghai\r\nChina<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u91cc\u65e2\u8c03\u7528\u4e86\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u4e5f\u8c03\u7528\u4e86\u7236\u7c7b\u7684\u53d8\u91cf\u3002\u82e5\u4e0d\u8c03\u7528\u7236\u7c7b\u65b9\u6cd5 value()\uff0c\u53ea\u8c03\u7528\u7236\u7c7b\u53d8\u91cf name \u7684\u8bdd\uff0c\u5219\u7236\u7c7b name \u503c\u4e3a\u9ed8\u8ba4\u503c null\u3002<\/p>\n

3.\u5f15\u7528\u6784\u9020\u51fd\u6570<\/strong><\/p>\n

    \n
  1. super(\u53c2\u6570)\uff1a\u8c03\u7528\u7236\u7c7b\u4e2d\u7684\u67d0\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff08\u5e94\u8be5\u4e3a\u6784\u9020\u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\uff09\u3002<\/li>\n
  2. this(\u53c2\u6570)\uff1a\u8c03\u7528\u672c\u7c7b\u4e2d\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684\u6784\u9020\u51fd\u6570\uff08\u5e94\u8be5\u4e3a\u6784\u9020\u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\uff09\u3002<\/li>\n<\/ol>\n

    \u5b9e\u4f8b<\/strong><\/p>\n

    class Person { \r\n    public static void prt(String s) { \r\n       System.out.println(s); \r\n    } \r\n   \r\n    Person() { \r\n       prt(\"\u7236\u7c7b\u00b7\u65e0\u53c2\u6570\u6784\u9020\u65b9\u6cd5\uff1a \"+\"A Person.\"); \r\n    }\/\/\u6784\u9020\u65b9\u6cd5(1) \r\n    \r\n    Person(String name) { \r\n       prt(\"\u7236\u7c7b\u00b7\u542b\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\uff1a \"+\"A person's name is \" + name); \r\n    }\/\/\u6784\u9020\u65b9\u6cd5(2) \r\n} \r\n    \r\npublic class Chinese extends Person { \r\n    Chinese() { \r\n       super(); \/\/ \u8c03\u7528\u7236\u7c7b\u6784\u9020\u65b9\u6cd5\uff081\uff09 \r\n       prt(\"\u5b50\u7c7b\u00b7\u8c03\u7528\u7236\u7c7b\"\u65e0\u53c2\u6570\u6784\u9020\u65b9\u6cd5\"\uff1a \"+\"A chinese coder.\"); \r\n    } \r\n    \r\n    Chinese(String name) { \r\n       super(name);\/\/ \u8c03\u7528\u7236\u7c7b\u5177\u6709\u76f8\u540c\u5f62\u53c2\u7684\u6784\u9020\u65b9\u6cd5\uff082\uff09 \r\n       prt(\"\u5b50\u7c7b\u00b7\u8c03\u7528\u7236\u7c7b\"\u542b\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\"\uff1a \"+\"his name is \" + name); \r\n    } \r\n    \r\n    Chinese(String name, int age) { \r\n       this(name);\/\/ \u8c03\u7528\u5177\u6709\u76f8\u540c\u5f62\u53c2\u7684\u6784\u9020\u65b9\u6cd5\uff083\uff09 \r\n       prt(\"\u5b50\u7c7b\uff1a\u8c03\u7528\u5b50\u7c7b\u5177\u6709\u76f8\u540c\u5f62\u53c2\u7684\u6784\u9020\u65b9\u6cd5\uff1ahis age is \" + age); \r\n    } \r\n    \r\n    public static void main(String[] args) { \r\n       Chinese cn = new Chinese(); \r\n       cn = new Chinese(\"codersai\"); \r\n       cn = new Chinese(\"codersai\", 18); \r\n    } \r\n}<\/pre>\n

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

    \u7236\u7c7b\u00b7\u65e0\u53c2\u6570\u6784\u9020\u65b9\u6cd5\uff1a A Person.\r\n\u5b50\u7c7b\u00b7\u8c03\u7528\u7236\u7c7b\u201d\u65e0\u53c2\u6570\u6784\u9020\u65b9\u6cd5\u201c\uff1a A chinese coder.\r\n\u7236\u7c7b\u00b7\u542b\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\uff1a A person's name is codersai\r\n\u5b50\u7c7b\u00b7\u8c03\u7528\u7236\u7c7b\u201d\u542b\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\u201c\uff1a his name is codersai\r\n\u7236\u7c7b\u00b7\u542b\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\uff1a A person's name is codersai\r\n\u5b50\u7c7b\u00b7\u8c03\u7528\u7236\u7c7b\u201d\u542b\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\u201c\uff1a his name is codersai\r\n\u5b50\u7c7b\uff1a\u8c03\u7528\u5b50\u7c7b\u5177\u6709\u76f8\u540c\u5f62\u53c2\u7684\u6784\u9020\u65b9\u6cd5\uff1ahis age is 18<\/pre>\n

    \u4ece\u672c\u4f8b\u53ef\u4ee5\u770b\u5230\uff0c\u53ef\u4ee5\u7528 super \u548c this \u5206\u522b\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u548c\u672c\u7c7b\u4e2d\u5176\u4ed6\u5f62\u5f0f\u7684\u6784\u9020\u65b9\u6cd5\u3002<\/p>\n

    \u4f8b\u5b50\u4e2d Chinese \u7c7b\u7b2c\u4e09\u79cd\u6784\u9020\u65b9\u6cd5\u8c03\u7528\u7684\u662f\u672c\u7c7b\u4e2d\u7b2c\u4e8c\u79cd\u6784\u9020\u65b9\u6cd5\uff0c\u800c\u7b2c\u4e8c\u79cd\u6784\u9020\u65b9\u6cd5\u662f\u8c03\u7528\u7236\u7c7b\u7684\uff0c\u56e0\u6b64\u4e5f\u8981\u5148\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u518d\u8c03\u7528\u672c\u7c7b\u4e2d\u7b2c\u4e8c\u79cd\uff0c\u6700\u540e\u662f\u91cd\u5199\u7b2c\u4e09\u79cd\u6784\u9020\u65b9\u6cd5\u3002<\/p>\n

    super \u548c this\u7684\u5f02\u540c<\/strong><\/div>\n
      \n
    1. super(\u53c2\u6570)\uff1a\u8c03\u7528\u57fa\u7c7b\u4e2d\u7684\u67d0\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff08\u5e94\u8be5\u4e3a\u6784\u9020\u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\uff09<\/li>\n
    2. this(\u53c2\u6570)\uff1a\u8c03\u7528\u672c\u7c7b\u4e2d\u53e6\u4e00\u79cd\u5f62\u6210\u7684\u6784\u9020\u51fd\u6570\uff08\u5e94\u8be5\u4e3a\u6784\u9020\u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\uff09<\/li>\n
    3. super:\u3000\u5b83\u5f15\u7528\u5f53\u524d\u5bf9\u8c61\u7684\u76f4\u63a5\u7236\u7c7b\u4e2d\u7684\u6210\u5458\uff08\u7528\u6765\u8bbf\u95ee\u76f4\u63a5\u7236\u7c7b\u4e2d\u88ab\u9690\u85cf\u7684\u7236\u7c7b\u4e2d\u6210\u5458\u6570\u636e\u6216\u51fd\u6570\uff0c\u57fa\u7c7b\u4e0e\u6d3e\u751f\u7c7b\u4e2d\u6709\u76f8\u540c\u6210\u5458\u5b9a\u4e49\u65f6\u5982\uff1asuper.\u53d8\u91cf\u540d super.\u6210\u5458\u51fd\u6570\u636e\u540d\uff08\u5b9e\u53c2\uff09 this\uff1a\u5b83\u4ee3\u8868\u5f53\u524d\u5bf9\u8c61\u540d\uff08\u5728\u7a0b\u5e8f\u4e2d\u6613\u4ea7\u751f\u4e8c\u4e49\u6027\u4e4b\u5904\uff0c\u5e94\u4f7f\u7528 this \u6765\u6307\u660e\u5f53\u524d\u5bf9\u8c61\uff1b\u5982\u679c\u51fd\u6570\u7684\u5f62\u53c2\u4e0e\u7c7b\u4e2d\u7684\u6210\u5458\u6570\u636e\u540c\u540d\uff0c\u8fd9\u65f6\u9700\u7528 this \u6765\u6307\u660e\u6210\u5458\u53d8\u91cf\u540d\uff09<\/li>\n
    4. \u8c03\u7528super()\u5fc5\u987b\u5199\u5728\u5b50\u7c7b\u6784\u9020\u65b9\u6cd5\u7684\u7b2c\u4e00\u884c\uff0c\u5426\u5219\u7f16\u8bd1\u4e0d\u901a\u8fc7\u3002\u6bcf\u4e2a\u5b50\u7c7b\u6784\u9020\u65b9\u6cd5\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\uff0c\u90fd\u662f\u9690\u542b\u5730\u8c03\u7528 super()\uff0c\u5982\u679c\u7236\u7c7b\u6ca1\u6709\u8fd9\u79cd\u5f62\u5f0f\u7684\u6784\u9020\u51fd\u6570\uff0c\u90a3\u4e48\u5728\u7f16\u8bd1\u7684\u65f6\u5019\u5c31\u4f1a\u62a5\u9519\u3002<\/li>\n
    5. super() \u548c this() \u7c7b\u4f3c,\u533a\u522b\u662f\uff0csuper() \u4ece\u5b50\u7c7b\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\uff0cthis() \u5728\u540c\u4e00\u7c7b\u5185\u8c03\u7528\u5176\u5b83\u65b9\u6cd5\u3002<\/li>\n
    6. super() \u548c this() \u5747\u9700\u653e\u5728\u6784\u9020\u65b9\u6cd5\u5185\u7b2c\u4e00\u884c\u3002<\/li>\n
    7. \u5c3d\u7ba1\u53ef\u4ee5\u7528this\u8c03\u7528\u4e00\u4e2a\u6784\u9020\u5668\uff0c\u4f46\u5374\u4e0d\u80fd\u8c03\u7528\u4e24\u4e2a\u3002<\/li>\n
    8. this \u548c super \u4e0d\u80fd\u540c\u65f6\u51fa\u73b0\u5728\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u91cc\u9762\uff0c\u56e0\u4e3athis\u5fc5\u7136\u4f1a\u8c03\u7528\u5176\u5b83\u7684\u6784\u9020\u51fd\u6570\uff0c\u5176\u5b83\u7684\u6784\u9020\u51fd\u6570\u5fc5\u7136\u4e5f\u4f1a\u6709 super \u8bed\u53e5\u7684\u5b58\u5728\uff0c\u6240\u4ee5\u5728\u540c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u91cc\u9762\u6709\u76f8\u540c\u7684\u8bed\u53e5\uff0c\u5c31\u5931\u53bb\u4e86\u8bed\u53e5\u7684\u610f\u4e49\uff0c\u7f16\u8bd1\u5668\u4e5f\u4e0d\u4f1a\u901a\u8fc7\u3002<\/li>\n
    9. this() \u548c super() \u90fd\u6307\u7684\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\uff0c\u5747\u4e0d\u53ef\u4ee5\u5728 static \u73af\u5883\u4e2d\u4f7f\u7528\u3002\u5305\u62ec\uff1astatic \u53d8\u91cf,static \u65b9\u6cd5\uff0cstatic \u8bed\u53e5\u5757\u3002<\/li>\n
    10. \u4ece\u672c\u8d28\u4e0a\u8bb2\uff0cthis \u662f\u4e00\u4e2a\u6307\u5411\u672c\u5bf9\u8c61\u7684\u6307\u9488, \u7136\u800c super \u662f\u4e00\u4e2a Java \u5173\u952e\u5b57\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"

      this \u7684\u7528\u6cd5\u5728 Java \u4e2d\u5927\u4f53\u53ef\u4ee5\u5206\u4e3a3\u79cd\uff1a 1.\u666e\u901a\u7684\u76f4\u63a5\u5f15\u7528 \u8fd9\u79cd\u5c31\u4e0d\u7528\u8bb2\u4e86\uff0cthis \u76f8\u5f53\u4e8e\u662f\u6307\u5411 […]<\/p>\n","protected":false},"author":321,"featured_media":216862,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-216860","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\/216860","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\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=216860"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216860\/revisions"}],"predecessor-version":[{"id":216864,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216860\/revisions\/216864"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/216862"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=216860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=216860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=216860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}