{"id":2218,"date":"2025-03-02T10:40:44","date_gmt":"2025-03-02T02:40:44","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2218"},"modified":"2025-03-02T10:40:44","modified_gmt":"2025-03-02T02:40:44","slug":"java-%e6%9e%84%e9%80%a0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/02\/java-%e6%9e%84%e9%80%a0%e6%96%b9%e6%b3%95\/","title":{"rendered":"Java \u6784\u9020\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u5728 Java \u4e2d\uff0c<strong>\u6784\u9020\u65b9\u6cd5<\/strong>\uff08Constructor\uff09\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u5b83\u7528\u4e8e\u521d\u59cb\u5316\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u6784\u9020\u65b9\u6cd5\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u88ab\u81ea\u52a8\u8c03\u7528\uff0c\u7528\u6765\u8bbe\u7f6e\u5bf9\u8c61\u7684\u521d\u59cb\u72b6\u6001\u3002\u6784\u9020\u65b9\u6cd5\u4e0e\u7c7b\u540c\u540d\uff0c\u5e76\u4e14\u6ca1\u6709\u8fd4\u56de\u503c\u7c7b\u578b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u6784\u9020\u65b9\u6cd5\u7684\u57fa\u672c\u7279\u70b9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u540d\u79f0\u4e0e\u7c7b\u540d\u76f8\u540c<\/strong>\uff1a\u6784\u9020\u65b9\u6cd5\u7684\u540d\u79f0\u5fc5\u987b\u4e0e\u7c7b\u540d\u76f8\u540c\u3002<\/li>\n\n\n\n<li><strong>\u6ca1\u6709\u8fd4\u56de\u503c<\/strong>\uff1a\u6784\u9020\u65b9\u6cd5\u6ca1\u6709\u8fd4\u56de\u7c7b\u578b\uff0c\u4e0d\u7ba1\u662f <code>void<\/code> \u8fd8\u662f\u5176\u4ed6\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u8c03\u7528<\/strong>\uff1a\u6784\u9020\u65b9\u6cd5\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u81ea\u52a8\u88ab\u8c03\u7528\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u7528\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u4ee5\u91cd\u8f7d<\/strong>\uff1a\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u6709\u591a\u4e2a\u6784\u9020\u65b9\u6cd5\uff0c\u8fd9\u79f0\u4e3a\u6784\u9020\u65b9\u6cd5\u7684\u91cd\u8f7d\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u6784\u9020\u65b9\u6cd5\u7684\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u9ed8\u8ba4\u6784\u9020\u65b9\u6cd5\uff08\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\uff09<\/strong><\/h4>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709\u663e\u5f0f\u5b9a\u4e49\u6784\u9020\u65b9\u6cd5\uff0cJava \u4f1a\u4e3a\u7c7b\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7684\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\uff0c\u5b83\u4e0d\u4f1a\u505a\u4efb\u4f55\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u4ec5\u4ec5\u521b\u5efa\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u5e26\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<p>\u5f53\u9700\u8981\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u6307\u5b9a\u67d0\u4e9b\u521d\u59cb\u503c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\u3002\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u5c06\u4f20\u9012\u7684\u503c\u8d4b\u7ed9\u5bf9\u8c61\u7684\u6210\u5458\u53d8\u91cf\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u6784\u9020\u65b9\u6cd5\u7684\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>\u65e0\u53c2\u6784\u9020\u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<p>\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\u4e0d\u4f1a\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u9ed8\u8ba4\u521d\u59cb\u5316\u5bf9\u8c61\u65f6\u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n    String name;\n    int age;\n\n    \/\/ \u65e0\u53c2\u6784\u9020\u65b9\u6cd5\n    public Person() {\n        name = \"Unknown\";\n        age = 0;\n    }\n\n    public void display() {\n        System.out.println(\"Name: \" + name + \", Age: \" + age);\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        \/\/ \u4f7f\u7528\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\u521b\u5efa\u5bf9\u8c61\n        Person person1 = new Person();\n        person1.display();  \/\/ \u8f93\u51fa: Name: Unknown, Age: 0\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong>\u5e26\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<p>\u5e26\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\u5141\u8bb8\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u63d0\u4f9b\u503c\uff0c\u4ee5\u4fbf\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n    String name;\n    int age;\n\n    \/\/ \u5e26\u53c2\u6570\u6784\u9020\u65b9\u6cd5\n    public Person(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    public void display() {\n        System.out.println(\"Name: \" + name + \", Age: \" + age);\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        \/\/ \u4f7f\u7528\u5e26\u53c2\u6570\u6784\u9020\u65b9\u6cd5\u521b\u5efa\u5bf9\u8c61\n        Person person1 = new Person(\"Alice\", 30);\n        person1.display();  \/\/ \u8f93\u51fa: Name: Alice, Age: 30\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.3 <strong>\u6784\u9020\u65b9\u6cd5\u7684\u91cd\u8f7d<\/strong><\/h4>\n\n\n\n<p>\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u6709\u591a\u4e2a\u6784\u9020\u65b9\u6cd5\uff0c\u5b83\u4eec\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u53c2\u6570\u5217\u8868\uff0c\u79f0\u4e3a\u6784\u9020\u65b9\u6cd5\u91cd\u8f7d\u3002\u901a\u8fc7\u91cd\u8f7d\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u53c2\u6570\u6765\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n    String name;\n    int age;\n\n    \/\/ \u65e0\u53c2\u6784\u9020\u65b9\u6cd5\n    public Person() {\n        name = \"Unknown\";\n        age = 0;\n    }\n\n    \/\/ \u5e26\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\n    public Person(String name) {\n        this.name = name;\n        this.age = 0;\n    }\n\n    \/\/ \u5e26\u4e24\u4e2a\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\n    public Person(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    public void display() {\n        System.out.println(\"Name: \" + name + \", Age: \" + age);\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        \/\/ \u8c03\u7528\u4e0d\u540c\u7684\u6784\u9020\u65b9\u6cd5\u521b\u5efa\u5bf9\u8c61\n        Person person1 = new Person();\n        Person person2 = new Person(\"Alice\");\n        Person person3 = new Person(\"Bob\", 25);\n\n        person1.display();  \/\/ \u8f93\u51fa: Name: Unknown, Age: 0\n        person2.display();  \/\/ \u8f93\u51fa: Name: Alice, Age: 0\n        person3.display();  \/\/ \u8f93\u51fa: Name: Bob, Age: 25\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.4 <strong>\u6784\u9020\u65b9\u6cd5\u4e2d\u7684 <code>this<\/code> \u5173\u952e\u5b57<\/strong><\/h4>\n\n\n\n<p><code>this<\/code> \u5173\u952e\u5b57\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\u7528\u4e8e\u5f15\u7528\u5f53\u524d\u5bf9\u8c61\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u533a\u5206\u5c40\u90e8\u53d8\u91cf\u548c\u5b9e\u4f8b\u53d8\u91cf\uff08\u6210\u5458\u53d8\u91cf\uff09\u3002\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\uff0c<code>this<\/code> \u5e38\u7528\u4e8e\u5f15\u7528\u5f53\u524d\u5bf9\u8c61\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n    String name;\n    int age;\n\n    \/\/ \u6784\u9020\u65b9\u6cd5\u4f7f\u7528 this \u5173\u952e\u5b57\n    public Person(String name, int age) {\n        this.name = name;  \/\/ \u4f7f\u7528 this \u6765\u533a\u5206\u6210\u5458\u53d8\u91cf\u548c\u53c2\u6570\n        this.age = age;\n    }\n\n    public void display() {\n        System.out.println(\"Name: \" + name + \", Age: \" + age);\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Person person = new Person(\"Alice\", 30);\n        person.display();  \/\/ \u8f93\u51fa: Name: Alice, Age: 30\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u6784\u9020\u65b9\u6cd5\u7684\u8c03\u7528\u987a\u5e8f<\/strong><\/h3>\n\n\n\n<p>\u5f53\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u6784\u9020\u65b9\u6cd5\uff0cJava \u4f1a\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u6267\u884c\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5982\u679c\u7236\u7c7b\u6709\u6784\u9020\u65b9\u6cd5\uff0c\u5219\u5148\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u3002\u5982\u679c\u7236\u7c7b\u6ca1\u6709\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\uff0c\u5b50\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u5fc5\u987b\u663e\u5f0f\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u7136\u540e\u6267\u884c\u5f53\u524d\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u7236\u7c7b\u4e0e\u5b50\u7c7b\u7684\u6784\u9020\u65b9\u6cd5<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    String name;\n\n    \/\/ \u7236\u7c7b\u6784\u9020\u65b9\u6cd5\n    public Animal(String name) {\n        this.name = name;\n        System.out.println(\"Animal constructor called\");\n    }\n}\n\nclass Dog extends Animal {\n    String breed;\n\n    \/\/ \u5b50\u7c7b\u6784\u9020\u65b9\u6cd5\n    public Dog(String name, String breed) {\n        super(name);  \/\/ \u8c03\u7528\u7236\u7c7b\u6784\u9020\u65b9\u6cd5\n        this.breed = breed;\n        System.out.println(\"Dog constructor called\");\n    }\n\n    public void display() {\n        System.out.println(\"Name: \" + name + \", Breed: \" + breed);\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Dog dog = new Dog(\"Buddy\", \"Golden Retriever\");\n        dog.display();  \/\/ \u8f93\u51fa: Name: Buddy, Breed: Golden Retriever\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Animal constructor called\nDog constructor called\nName: Buddy, Breed: Golden Retriever<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u6784\u9020\u65b9\u6cd5\u7684\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6784\u9020\u65b9\u6cd5\u7684\u4f5c\u7528<\/strong>\uff1a\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u72b6\u6001\u3002<\/li>\n\n\n\n<li><strong>\u6784\u9020\u65b9\u6cd5\u7684\u7279\u70b9<\/strong>\uff1a\u540d\u79f0\u4e0e\u7c7b\u540d\u76f8\u540c\uff0c\u6ca1\u6709\u8fd4\u56de\u7c7b\u578b\uff0c\u53ef\u4ee5\u91cd\u8f7d\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>this<\/code> \u5173\u952e\u5b57<\/strong>\uff1a\u7528\u4e8e\u5f15\u7528\u5f53\u524d\u5bf9\u8c61\u7684\u6210\u5458\u53d8\u91cf\uff0c\u89e3\u51b3\u53c2\u6570\u4e0e\u6210\u5458\u53d8\u91cf\u540c\u540d\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u7236\u7c7b\u4e0e\u5b50\u7c7b\u6784\u9020\u65b9\u6cd5<\/strong>\uff1a\u5b50\u7c7b\u6784\u9020\u65b9\u6cd5\u4f1a\u9690\u5f0f\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u5982\u679c\u7236\u7c7b\u6ca1\u6709\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\uff0c\u5219\u5fc5\u987b\u663e\u5f0f\u8c03\u7528\u7236\u7c7b\u6784\u9020\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6784\u9020\u65b9\u6cd5\u662f Java \u4e2d\u975e\u5e38\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u7406\u89e3\u6784\u9020\u65b9\u6cd5\u5bf9\u4e8e\u6b63\u786e\u521b\u5efa\u548c\u521d\u59cb\u5316\u5bf9\u8c61\u975e\u5e38\u5173\u952e\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u4e2d\uff0c\u6784\u9020\u65b9\u6cd5\uff08Constructor\uff09\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u5b83\u7528\u4e8e\u521d\u59cb\u5316\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u6784\u9020\u65b9\u6cd5\u5728\u5bf9\u8c61 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-2218","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2218"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2218\/revisions"}],"predecessor-version":[{"id":2219,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2218\/revisions\/2219"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}