{"id":624,"date":"2023-12-24T18:01:01","date_gmt":"2023-12-24T10:01:01","guid":{"rendered":""},"modified":"2023-12-24T18:01:01","modified_gmt":"2023-12-24T10:01:01","slug":"\u91cd\u8f7d\u548c\u91cd\u5199\u6709\u4ec0\u4e48\u533a\u522b","status":"publish","type":"post","link":"https:\/\/mushiming.com\/624.html","title":{"rendered":"\u91cd\u8f7d\u548c\u91cd\u5199\u6709\u4ec0\u4e48\u533a\u522b"},"content":{"rendered":"

\u91cd\u8f7d\u548c\u91cd\u5199\u662fJava\u4e2d\u4e24\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u3002\u91cd\u8f7d\u53ef\u4ee5\u5b9e\u73b0\u672c\u7c7b\u5185\u7684\u65b9\u6cd5\u591a\u6001\u6027\uff0c\u91cd\u5199\u53ef\u4ee5\u5b9e\u73b0\u5b50\u7c7b\u6216\u5b9e\u73b0\u7c7b\u7684\u591a\u6001\u6027\u3002<\/p>\n

\u4e00\u3001\u91cd\u8f7d<\/strong><\/p>\n

\u5728\u81ea\u7136\u8bed\u8a00\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u51fa\u73b0\u201c\u4e00\u8bcd\u591a\u4e49\u201d\u7684\u73b0\u8c61\u3002\u6bd4\u5982\uff1a\u201c\u58a8\u6c34\u201d\uff0c\u5b83\u53ef\u4ee5\u6307\u4e00\u79cd\u542b\u6709\u8272\u7d20\u6216\u67d3\u6599\u7684\u6db2\u4f53\uff0c\u58a8\u6c34\u88ab\u7528\u4e8e\u4e66\u5199\u6216\u7ed8\u753b\uff1b\u8fd8\u53ef\u4ee5\u6307\u4eba\u7684\u6587\u5316\u3001\u77e5\u8bc6\uff0c\u201c\u80f8\u65e0\u534a\u70b9\u58a8\u201d\u4fbf\u6307\u4eba\u7684\u77e5\u8bc6\u5f88\u532e\u4e4f\u3002\u4e00\u4e2a\u8bcd\u8bed\u5982\u679c\u6709\u591a\u91cd\u542b\u4e49\uff0c\u90a3\u4e48\u5c31\u8bf4\u8be5\u8bcd\u8bed\u88ab\u91cd\u8f7d\u4e86\uff0c\u5177\u4f53\u4ee3\u8868\u4ec0\u4e48\u542b\u4e49\u9700\u8981\u7ed3\u5408\u5177\u4f53\u7684\u573a\u666f\u3002\u5728Java\u8bed\u8a00\u4e2d\uff0c\u4e5f\u5b58\u5728\u91cd\u8f7d\uff0c\u4e0d\u8fc7\u5b83\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n

1.\u65b9\u6cd5\u540d\u5fc5\u987b\u76f8\u540c\uff1b<\/p>\n

2.\u53c2\u6570\u5217\u8868\u5fc5\u987b\u4e0d\u540c\uff08\u53c2\u6570\u7684\u4e2a\u6570\u4e0d\u540c\u3001\u53c2\u6570\u7684\u7c7b\u578b\u4e0d\u540c\u3001\u7c7b\u578b\u7684\u6b21\u5e8f\u5fc5\u987b\u4e0d\u540c\uff09<\/p>\n

    public static void main(String[] args) {\n        print(12);\n        print(\"1234\");\n        print(\"hello Java\uff01\");\n    }\n    public static void print(int i){\n        System.out.println(\"\u6253\u5370\u6574\u578b\u503c\uff1a\"+i);\n    }\n    public static void print(String s){\n        System.out.println(\"\u6253\u5370\u5b57\u7b26\u4e32\u7c7b\u578b\u503c\uff1a\"+s);\n    }\n    public static void print(String s1,String s2){\n        System.out.println(\"\u6253\u5370\u5b57\u7b26\u4e32\u7c7b\u578b\u503c1\"+s1+\";\u5b57\u7b26\u4e32\u7c7b\u578b\u503c2\uff1a\"+s2);\n    }\n\n   \/\/\u8f93\u51fa\u7ed3\u679c\n   \/\/\u6253\u5370\u6574\u578b\u503c\uff1a12\n   \/\/\u6253\u5370\u5b57\u7b26\u4e32\u7c7b\u578b\u503c\uff1a1234\n   \/\/\u6253\u5370\u5b57\u7b26\u4e32\u7c7b\u578b\u503c\uff1ahello Java\uff01<\/code><\/pre>\n

\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u4ee3\u7801\u65f6\uff0c\u4f1a\u5bf9\u5b9e\u53c2\u8fdb\u884c\u63a8\u6f14\uff0c\u4f1a\u6839\u636e\u63a8\u6f14\u7684\u7ed3\u679c\u6765\u786e\u5b9a\u8c03\u7528\u90a3\u4e2a\u65b9\u6cd5\u3002<\/p>\n

\u6ce8\u610f\uff1a\u91cd\u8f7d\u4e0e\u8fd4\u56de\u503c\u7c7b\u578b\u662f\u5426\u76f8\u540c\u65e0\u5173\uff1b\u4e24\u4e2a\u65b9\u6cd5\u5982\u679c\u4ec5\u4ec5\u53ea\u662f\u56e0\u4e3a\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u540c\uff0c\u662f\u4e0d\u80fd\u6784\u6210\u91cd\u8f7d\u7684\u3002\u5177\u4f53\u9519\u8bef\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

public static void main(String[] args) {\n    int a = 10;\n    int b = 20;\n    int ret = add(a, b);\n    System.out.println(\"ret = \" + ret);\n }\npublic static int add(int x, int y) {\n    return x + y;\n }\npublic static double add(int x, int y) {\n   return x + y;\n }\n \n\n\/\/\u7f16\u8bd1\u9519\u8bef\nTest.java:\u5df2\u5728\u7c7b TestDemo\u4e2d\u5b9a\u4e49\u4e86\u65b9\u6cd5 add(int,int)\n      public static double add(int x,int y){\n1\u4e2a\u9519\u8bef<\/code><\/pre>\n

\"\u91cd\u8f7d\u548c\u91cd\u5199\u6709\u4ec0\u4e48\u533a\u522b<\/p>\n<\/p>\n

\u4e8c\u3001\u91cd\u5199<\/strong><\/p>\n

\n \u91cd\u5199(override)\uff1a\u4e5f\u79f0\u4e3a\u8986\u76d6\u3002\u91cd\u5199\u662f\u5b50\u7c7b\u5bf9\u7236\u7c7b\u975e\u9759\u6001\u3001\u975eprivate\u4fee\u9970\uff0c\u975efinal\u4fee\u9970\uff0c\u975e\u6784\u9020\u65b9\u6cd5\u7b49\u7684\u5b9e\u73b0\u8fc7\u7a0b\u8fdb \u884c\u91cd\u65b0\u7f16\u5199 , \u8fd4\u56de\u503c\u548c\u5f62\u53c2\u90fd\u4e0d\u80fd\u6539\u53d8\u3002\u5373\u5916\u58f3\u4e0d\u53d8\uff0c\u6838\u5fc3\u91cd\u5199\uff01 \u91cd\u5199\u7684\u597d\u5904\u5728\u4e8e\u5b50\u7c7b\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u5b9a\u4e49\u7279\u5b9a\u4e8e\u81ea\u5df1\u7684\u884c\u4e3a\u3002 \u4e5f\u5c31\u662f\u8bf4\u5b50\u7c7b\u80fd\u591f\u6839\u636e\u9700\u8981\u5b9e\u73b0\u7236\u7c7b\u7684\u65b9\u6cd5\u3002\n<\/div>\n
\n \u65b9\u6cd5\u91cd\u5199\u7684\u89c4\u5219<\/strong>\n<\/div>\n
\n 1.\u5b50\u7c7b\u5728\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u65f6\uff0c\u4e00\u822c\u5fc5\u987b\u4e0e\u7236\u7c7b\u65b9\u6cd5\u539f\u578b\u4e00\u81f4\uff1a\u4fee\u9970\u7b26 \u8fd4\u56de\u503c\u7c7b\u578b \u65b9\u6cd5\u540d(\u53c2\u6570\u5217\u8868)\u8981\u5b8c\u5168\u4e00\u81f4\u3002\n<\/div>\n
\n 2.JDK7\u4ee5\u540e\uff0c\u88ab\u91cd\u5199\u7684\u65b9\u6cd5\u8fd4\u56de\u503c\u7c7b\u578b\u53ef\u4ee5\u4e0d\u540c\uff0c\u4f46\u662f\u5fc5\u987b\u662f\u5177\u6709\u7236\u5b50\u5173\u7cfb\u7684\u8bbf\u95ee\u6743\u9650\u4e0d\u80fd\u6bd4\u7236\u7c7b\u4e2d\u88ab\u91cd\u5199\u7684\u65b9\u6cd5\u7684\u8bbf\u95ee\u6743\u9650\u66f4\u4f4e\u3002\u4f8b\u5982\uff1a\u5982\u679c\u7236\u7c7b\u65b9\u6cd5\u88abpublic\u4fee\u9970\uff0c\u5219\u5b50\u7c7b\u4e2d\u91cd\u5199\u8be5\u65b9\u6cd5\u5c31\u4e0d\u80fd\u58f0\u660e\u4e3a protected\u3002\n<\/div>\n
\n 3.\u7236\u7c7b\u88abstatic\u3001private\u4fee\u9970\u7684\u65b9\u6cd5\u3001\u6784\u9020\u65b9\u6cd5\u90fd\u4e0d\u80fd\u88ab\u91cd\u5199\u3002\n<\/div>\n
\n 4.\u5b50\u7c7b\u548c\u7236\u7c7b\u5728\u540c\u4e00\u4e2a\u5305\u4e2d\uff0c\u90a3\u4e48\u5b50\u7c7b\u53ef\u4ee5\u91cd\u5199\u7236\u7c7b\u6240\u6709\u65b9\u6cd5\uff0c\u9664\u4e86\u58f0\u660e\u4e3a private \u548c fifinal \u7684\u65b9\u6cd5\u3002\n<\/div>\n
\n 5.\u5b50\u7c7b\u548c\u7236\u7c7b\u4e0d\u5728\u540c\u4e00\u4e2a\u5305\u4e2d\uff0c\u90a3\u4e48\u5b50\u7c7b\u53ea\u80fd\u591f\u91cd\u5199\u7236\u7c7b\u7684\u58f0\u660e\u4e3a public \u548c protected \u7684\u975e fifinal \u65b9\u6cd5\u3002\n<\/div>\n
\n 6.\u91cd\u5199\u7684\u65b9\u6cd5, \u53ef\u4ee5\u4f7f\u7528 @Override \u6ce8\u89e3\u6765\u663e\u5f0f\u6307\u5b9a. \u6709\u4e86\u8fd9\u4e2a\u6ce8\u89e3\u80fd\u5e2e\u6211\u4eec\u8fdb\u884c\u4e00\u4e9b\u5408\u6cd5\u6027\u6821\u9a8c. \u4f8b\u5982\u4e0d\u5c0f\u5fc3\u5c06\u65b9\u6cd5\u540d\u5b57\u62fc\u5199\u9519\u4e86 (\u6bd4\u5982\u5199\u6210 aet), \u90a3\u4e48\u6b64\u65f6\u7f16\u8bd1\u5668\u5c31\u4f1a\u53d1\u73b0\u7236\u7c7b\u4e2d\u6ca1\u6709 aet \u65b9\u6cd5, \u5c31\u4f1a\u7f16\u8bd1\u62a5\u9519, \u63d0\u793a\u65e0\u6cd5\u6784\u6210\u91cd\u5199\u3002\n<\/div>\n
<\/div>\n
\n \u91cd\u5199\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/div>\n
\n
\/\/Animal.java\npublic class Animal {\n    String name;\n    int age;\n\n    public Animal(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    public void eat() {\n        System.out.println(name + \"\u5403\u996d\");\n    }\n}\n\/\/Cat.java\npublic class Cat extends Animal {\n    public Cat(String name, int age) {\n        super(name, age);\n    }\n\n    @Override\n    public void eat() {\n        System.out.println(name + \"\u5403\u9c7c\");\n    }\n}\n\n\/\/Dog.java\npublic class Dog extends Animal {\n    public Dog(String name, int age){\n        super(name, age);\n    }\n\n    @Override\n    public void eat(){\n       System.out.println(name+\"\u5403\u9aa8\u5934\");\n    }\n}\n\/\/TestAnmal.java\npublic class TestAnimal {\n    \/\/ \u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u4ee3\u7801\u65f6\uff0c\u5e76\u4e0d\u77e5\u9053\u8981\u8c03\u7528Dog \u8fd8\u662f Cat \u4e2deat\u7684\u65b9\u6cd5\n    \/\/ \u7b49\u7a0b\u5e8f\u8fd0\u884c\u8d77\u6765\u540e\uff0c\u5f62\u53c2a\u5f15\u7528\u7684\u5177\u4f53\u5bf9\u8c61\u786e\u5b9a\u540e\uff0c\u624d\u77e5\u9053\u8c03\u7528\u90a3\u4e2a\u65b9\u6cd5\n    \/\/ \u6ce8\u610f\uff1a\u6b64\u5904\u7684\u5f62\u53c2\u7c7b\u578b\u5fc5\u987b\u65f6\u7236\u7c7b\u7c7b\u578b\u624d\u53ef\u4ee5\n    public static void eat(Animal a) {\n        a.eat();\n    }\n\n    public static void main(String[] args) {\n        Cat cat = new Cat(\"\u5c0f\u9ec4\", 2);\n        Dog dog = new Dog(\"\u5927\u9ec4\", 3);\n        eat(cat);\n        eat(dog);\n    }\n}\n\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a\n\/\/\u5c0f\u9ec4\u5403\u9c7c\n\/\/\u5927\u9ec4\u5403\u9aa8\u5934<\/code><\/pre>\n<\/p>\n<\/div>\n
<\/div>\n
<\/div>\n
\n \u4e09\u3001\u91cd\u8f7d\u548c\u91cd\u5199\u7684\u533a\u522b<\/strong>\n<\/div>\n
\n\n\n\n\n\n\n\n
\u533a\u522b\u70b9<\/td>\n\u91cd\u8f7d      <\/td>\n\u91cd\u5199<\/td>\n<\/tr>\n
\u53c2\u6570\u5217\u8868<\/td>\n\u5fc5\u987b\u4fee\u6539<\/td>\n\u4e00\u5b9a\u4e0d\u80fd\u4fee\u6539<\/td>\n<\/tr>\n
\u8fd4\u56de\u7c7b\u578b<\/td>\n\u53ef\u4ee5\u4fee\u6539<\/td>\n\u4e00\u5b9a\u4e0d\u80fd\u4fee\u6539<\/td>\n<\/tr>\n
\u8bbf\u95ee\u9650\u5b9a\u7b26<\/td>\n\u53ef\u4ee5\u4fee\u6539<\/td>\n\u4e00\u5b9a\u4e0d\u80fd\u505a\u66f4\u4e25\u683c\u7684\u9650\u5236\uff08\u53ef\u4ee5\u964d\u4f4e\u9650\u5236\uff09<\/td>\n<\/tr>\n
\u5f02\u5e38<\/td>\n\u53ef\u4ee5\u4fee\u6539<\/td>\n\u53ef\u4ee5\u51cf\u5c11\u6216\u8005\u5220\u9664\uff0c\u4e00\u5b9a\u4e0d\u80fd\u629b\u51fa\u65b0\u7684\u6216\u8005\u66f4\u5e7f\u7684\u5f02\u5e38<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\u91cd\u8f7d\u548c\u91cd\u5199\u6709\u4ec0\u4e48\u533a\u522b\u91cd\u8f7d\u548c\u91cd\u5199\u662fJava\u4e2d\u4e24\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u3002\u91cd\u8f7d\u53ef\u4ee5\u5b9e\u73b0\u672c\u7c7b\u5185\u7684\u65b9\u6cd5\u591a\u6001\u6027\uff0c\u91cd\u5199\u53ef\u4ee5\u5b9e\u73b0\u5b50\u7c7b\u6216\u5b9e\u73b0\u7c7b\u7684\u591a\u6001...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/624"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=624"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/624\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}