{"id":71,"date":"2015-03-30T10:46:23","date_gmt":"2015-03-30T02:46:23","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=71"},"modified":"2026-08-18T12:09:24","modified_gmt":"2026-08-18T04:09:24","slug":"java%e5%af%b9%e8%b1%a1%e5%92%8c%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=71","title":{"rendered":"Java\u5bf9\u8c61\u548c\u7c7b"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">Java \u662f\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\uff0c\u56f4\u7ed5<strong style=\"color: #4870ac;\">\u7c7b<\/strong>\u548c<strong style=\"color: #4870ac;\">\u5bf9\u8c61<\/strong>\u7ec4\u7ec7\u4ee3\u7801\u3002\u5e38\u89c1\u6982\u5ff5\u8fd8\u6709\u5b9e\u4f8b\u3001\u65b9\u6cd5\u3001\u5c5e\u6027\u3001\u6d88\u606f\u3001\u5c01\u88c5\u3001\u7ee7\u627f\u3001\u591a\u6001\u3001\u62bd\u8c61\u3002\u672c\u8282\u53ea\u628a\u5bf9\u8c61\u548c\u7c7b\u8bb2\u6e05\u695a\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5bf9\u8c61\u662f\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u6709\u72b6\u6001\u548c\u884c\u4e3a\u3002\u4e00\u6761\u72d7\u662f\u5bf9\u8c61\uff1a\u72b6\u6001\u53ef\u4ee5\u662f\u989c\u8272\u3001\u540d\u5b57\u3001\u54c1\u79cd\uff0c\u884c\u4e3a\u53ef\u4ee5\u662f\u6447\u5c3e\u5df4\u3001\u53eb\u3001\u5403\u3002\u7c7b\u5219\u662f\u6a21\u677f\uff0c\u63cf\u8ff0\u8fd9\u4e00\u7c7b\u5bf9\u8c61\u5171\u6709\u7684\u884c\u4e3a\u548c\u72b6\u6001\u3002<\/p>\n<p><!--more--><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u73b0\u5b9e\u5bf9\u8c61\u548c\u8f6f\u4ef6\u5bf9\u8c61<\/h2>\n<p style=\"margin: 1em 0px;\">\u8eab\u8fb9\u5230\u5904\u662f\u5bf9\u8c61\uff1a\u8f66\u3001\u72d7\u3001\u4eba\u3002\u5b83\u4eec\u90fd\u6709\u72b6\u6001\u548c\u884c\u4e3a\u3002\u8f6f\u4ef6\u5bf9\u8c61\u5f88\u50cf\uff1a\u72b6\u6001\u7528<strong style=\"color: #4870ac;\">\u5c5e\u6027<\/strong>\uff08\u6210\u5458\u53d8\u91cf\uff09\u4fdd\u5b58\uff0c\u884c\u4e3a\u7528<strong style=\"color: #4870ac;\">\u65b9\u6cd5<\/strong>\u4f53\u73b0\u3002\u65b9\u6cd5\u8d1f\u8d23\u6539\u5185\u90e8\u72b6\u6001\uff0c\u5bf9\u8c61\u4e4b\u95f4\u4e5f\u901a\u8fc7\u65b9\u6cd5\u4e92\u76f8\u8c03\u7528\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u7c7b\uff1a\u521b\u5efa\u5bf9\u8c61\u7684\u6a21\u677f<\/h2>\n<p style=\"margin: 1em 0px;\">\u4e00\u4e2a\u7b80\u5355\u7684 Dog \u7c7b\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class Dog {\n    String breed;\n    int age;\n    String color;\n\n    void barking() {\n    }\n\n    void hungry() {\n    }\n\n    void sleeping() {\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7c7b\u91cc\u53ef\u4ee5\u51fa\u73b0\u4e09\u7c7b\u53d8\u91cf\uff1a<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u79cd\u7c7b<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5b9a\u4e49\u4f4d\u7f6e<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u751f\u547d\u5468\u671f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5c40\u90e8\u53d8\u91cf<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u65b9\u6cd5\u3001\u6784\u9020\u65b9\u6cd5\u6216\u8bed\u53e5\u5757\u5185<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u65b9\u6cd5\u7ed3\u675f\u5c31\u9500\u6bc1\uff0c\u4f7f\u7528\u524d\u5fc5\u987b\u8d4b\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6210\u5458\u53d8\u91cf\uff08\u5b9e\u4f8b\u53d8\u91cf\uff09<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7c7b\u4e2d\u3001\u65b9\u6cd5\u5916\uff0c\u65e0 static<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u968f\u5bf9\u8c61\u521b\u5efa\u800c\u5206\u914d\uff0c\u65b9\u6cd5\/\u6784\u9020\u65b9\u6cd5\u53ef\u8bbf\u95ee<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7c7b\u53d8\u91cf<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7c7b\u4e2d\u3001\u65b9\u6cd5\u5916\uff0c\u5e26 static<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5c5e\u4e8e\u7c7b\u672c\u8eab\uff0c\u6240\u6709\u5b9e\u4f8b\u5171\u4eab<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin: 1em 0px;\">\u4e0a\u9762\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">barking()<\/code>\u3001<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">hungry()<\/code>\u3001<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">sleeping()<\/code> \u90fd\u662f Dog \u7684\u65b9\u6cd5\u3002\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u6709\u591a\u4e2a\u65b9\u6cd5\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u6784\u9020\u65b9\u6cd5<\/h2>\n<p style=\"margin: 1em 0px;\">\u6bcf\u4e2a\u7c7b\u90fd\u6709\u6784\u9020\u65b9\u6cd5\u3002\u4f60\u4e0d\u5199\uff0c\u7f16\u8bd1\u5668\u4f1a\u7ed9\u4e00\u4e2a<strong style=\"color: #4870ac;\">\u9ed8\u8ba4\u65e0\u53c2\u6784\u9020<\/strong>\u3002\u521b\u5efa\u5bf9\u8c61\u65f6\u81f3\u5c11\u4f1a\u8c03\u7528\u4e00\u4e2a\u6784\u9020\u65b9\u6cd5\u3002\u540d\u5b57\u5fc5\u987b\u548c\u7c7b\u540d\u76f8\u540c\uff0c\u53ef\u4ee5\u91cd\u8f7d\u591a\u4e2a\u7248\u672c\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class Puppy {\n    public Puppy() {\n    }\n\n    public Puppy(String name) {\n        \/\/ \u8fd9\u4e2a\u6784\u9020\u5668\u4ec5\u6709\u4e00\u4e2a\u53c2\u6570\uff1aname\n    }\n}<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001\u7528 new \u521b\u5efa\u5bf9\u8c61<\/h2>\n<p style=\"margin: 1em 0px;\">\u4e09\u6b65\uff1a<strong style=\"color: #4870ac;\">\u58f0\u660e<\/strong>\u7c7b\u578b\u548c\u540d\u5b57 \u2192 \u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">new<\/code> <strong style=\"color: #4870ac;\">\u5b9e\u4f8b\u5316<\/strong> \u2192 \u8c03\u7528\u6784\u9020\u65b9\u6cd5<strong style=\"color: #4870ac;\">\u521d\u59cb\u5316<\/strong>\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class Puppy {\n    public Puppy(String name) {\n        System.out.println(\"Passed Name is :\" + name);\n    }\n    public static void main(String[] args) {\n        Puppy myPuppy = new Puppy(\"tommy\");\n    }\n}\n\/\/ \u8f93\u51fa\uff1aPassed Name is :tommy<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8bbf\u95ee\u6210\u5458\u7684\u5199\u6cd5\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/* \u5b9e\u4f8b\u5316\u5bf9\u8c61 *\/\nObjectReference = new Constructor();\n\/* \u8bbf\u95ee\u5176\u4e2d\u7684\u53d8\u91cf *\/\nObjectReference.variableName;\n\/* \u8bbf\u95ee\u7c7b\u4e2d\u7684\u65b9\u6cd5 *\/\nObjectReference.MethodName();<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5b8c\u6574\u4f8b\u5b50\uff1a\u6784\u9020\u65f6\u6253\u5370\u540d\u5b57\uff0c\u518d\u7528 setter \/ getter \u64cd\u4f5c\u5e74\u9f84\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8bfb\u5b57\u6bb5\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class Puppy {\n    int puppyAge;\n    public Puppy(String name) {\n        System.out.println(\"Passed Name is :\" + name);\n    }\n    public void setAge(int age) {\n        puppyAge = age;\n    }\n    public int getAge() {\n        System.out.println(\"Puppy's age is :\" + puppyAge);\n        return puppyAge;\n    }\n    public static void main(String[] args) {\n        Puppy myPuppy = new Puppy(\"tommy\");\n        myPuppy.setAge(2);\n        myPuppy.getAge();\n        System.out.println(\"Variable Value :\" + myPuppy.puppyAge);\n    }\n}\n\/\/ Passed Name is :tommy\n\/\/ Puppy's age is :2\n\/\/ Variable Value :2<\/code><\/pre>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u7c7b\u662f\u6a21\u677f\uff0c\u5bf9\u8c61\u662f\u5b9e\u4f8b\uff1bnew \u4f1a\u8c03\u7528\u6784\u9020\u65b9\u6cd5\uff0c\u72b6\u6001\u653e\u5b57\u6bb5\u3001\u884c\u4e3a\u653e\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Java \u662f\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\uff0c\u56f4\u7ed5\u7c7b\u548c\u5bf9\u8c61\u7ec4\u7ec7\u4ee3\u7801\u3002\u5e38\u89c1\u6982\u5ff5\u8fd8\u6709\u5b9e\u4f8b\u3001\u65b9\u6cd5\u3001\u5c5e\u6027\u3001\u6d88\u606f\u3001\u5c01\u88c5\u3001\u7ee7\u627f\u3001\u591a\u6001\u3001\u62bd\u8c61\u3002\u672c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[43],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/71"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=71"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions\/370"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}