{"id":89,"date":"2015-03-30T11:46:44","date_gmt":"2015-03-30T03:46:44","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=89"},"modified":"2026-08-18T12:09:25","modified_gmt":"2026-08-18T04:09:25","slug":"java%e5%a4%9a%e6%80%81%e5%af%b9%e8%b1%a1%e7%9a%84%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=89","title":{"rendered":"Java\u591a\u6001\u5bf9\u8c61\u7684\u7c7b\u578b\u8f6c\u6362"},"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;\">\u8fd9\u91cc\u8bf4\u7684\u5bf9\u8c61\u7c7b\u578b\u8f6c\u6362\uff0c\u53ea\u53d1\u751f\u5728<strong style=\"color: #4870ac;\">\u6709\u7ee7\u627f\u5173\u7cfb<\/strong>\u7684\u7c7b\u578b\u4e4b\u95f4\u3002\u6ca1\u6709\u7ee7\u627f\u5173\u7cfb\u5374\u5f3a\u5236\u8f6c\uff0c\u8fd0\u884c\u65f6\u4f1a\u629b <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;\">java.lang.ClassCastException<\/code>\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7ee7\u627f\u94fe\u4e0a\uff0c\u5b50\u7c7b\u8f6c\u6210\u7236\u7c7b\u53eb<strong style=\"color: #4870ac;\">\u5411\u4e0a\u8f6c\u578b<\/strong>\uff0c\u7236\u7c7b\u8f6c\u6210\u5b50\u7c7b\u53eb<strong style=\"color: #4870ac;\">\u5411\u4e0b\u8f6c\u578b<\/strong>\u3002\u5411\u4e0a\u8f6c\u578b\u5f88\u5e38\u89c1\uff1a\u53d8\u91cf\u5199\u6210\u7236\u7c7b\uff0c\u5b9e\u9645\u5f15\u7528\u5b50\u7c7b\uff0c\u8fd0\u884c\u65f6\u9760\u52a8\u6001\u7ed1\u5b9a\u8c03\u7528\u5b50\u7c7b\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u591a\u6001\u3002\u82e5\u8981\u505a\u7236\u7c7b\u6ca1\u6709\u7684\u4e8b\uff0c\u518d\u628a\u5f15\u7528\u8f6c\u56de\u5b50\u7c7b\uff0c\u8c03\u7528\u5b50\u7c7b\u81ea\u5df1\u7684\u65b9\u6cd5\uff0c\u8fd9\u5c31\u662f\u5411\u4e0b\u8f6c\u578b\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;\">\u4e00\u3001\u5411\u4e0a\u518d\u5411\u4e0b<\/h2>\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;\">class People {\n    void say() {\n        System.out.println(\"\u5927\u5bb6\u597d\uff0c\u6211\u662f\u826f\u6c11\");\n    }\n}\nclass Teacher extends People {\n    void say() {\n        System.out.println(\"\u5927\u5bb6\u597d\uff0c\u6211\u662f\u4e00\u540d\u8001\u5e08\");\n    }\n    void lecturing() {\n        System.out.println(\"\u6211\u662f\u4e00\u540d\u8001\u5e08,\u6211\u7684\u5de5\u4f5c\u662f\u8bb2\u8bfe\");\n    }\n}\npublic class Demo {\n    public static void main(String[] args) {\n        People obj = new Teacher();   \/\/ \u5411\u4e0a\u8f6c\u578b\n        Teacher tbj = (Teacher) obj;  \/\/ \u5411\u4e0b\u8f6c\u578b\uff0c\u5b9e\u9645\u5bf9\u8c61\u672c\u6765\u5c31\u662f Teacher\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e0d\u80fd\u628a\u4e00\u4e2a<strong style=\"color: #4870ac;\">\u771f\u6b63\u7684\u7236\u7c7b\u5bf9\u8c61<\/strong>\u5f3a\u8f6c\u6210\u5b50\u7c7b\u3002\u539f\u6587\u8bf4\u300c\u5fc5\u987b\u5148\u5411\u4e0a\u518d\u5411\u4e0b\u300d\u5bb9\u6613\u542c\u6210\u6d41\u7a0b\u4eea\u5f0f\uff0c\u66f4\u51c6\u786e\u7684\u8bf4\u6cd5\u662f\uff1a<strong style=\"color: #4870ac;\">\u5806\u4e0a\u5b9e\u9645\u7c7b\u578b\u5fc5\u987b\u5df2\u7ecf\u662f\u76ee\u6807\u5b50\u7c7b\uff08\u6216\u5176\u5b50\u7c7b\uff09<\/strong>\uff0c\u5f3a\u5236\u8f6c\u6362\u624d\u5b89\u5168\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 Demo {\n    public static void main(String[] args) {\n        People obj = new People();\n        Teacher tbj = (Teacher) obj; \/\/ \u8fd0\u884c\u65f6\u62a5 ClassCastException\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;\">\u4e8c\u3001\u8f6c\u578b\u540e\u80fd\u8c03\u4ec0\u4e48<\/h2>\n<p style=\"margin: 1em 0px;\">\u6307\u5411\u5b50\u7c7b\u7684\u7236\u7c7b\u5f15\u7528\uff0c\u7f16\u8bd1\u671f\u53ea\u80fd\u770b\u89c1\u7236\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u5b50\u7c7b\u591a\u51fa\u6765\u7684\u65b9\u6cd5\uff08\u5305\u62ec\u91cd\u8f7d\u51fa\u6765\u3001\u7236\u7c7b\u6ca1\u6709\u7684\u90a3\u4e00\u7248\uff09\u4e0d\u80fd\u76f4\u63a5\u8c03\u3002\u82e5\u5b50\u7c7b<strong style=\"color: #4870ac;\">\u8986\u76d6<\/strong>\u4e86\u7236\u7c7b\u65b9\u6cd5\uff0c\u8c03\u7528\u65f6\u4e00\u5b9a\u8d70\u5b50\u7c7b\uff08\u52a8\u6001\u7ed1\u5b9a\uff09\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;\">class People {\n    void say() {\n        System.out.println(\"\u5927\u5bb6\u597d\uff0c\u6211\u662f\u826f\u6c11\");\n    }\n}\nclass Teacher extends People {\n    void say() {\n        System.out.println(\"\u5927\u5bb6\u597d\uff0c\u6211\u662f\u4e00\u540d\u8001\u5e08\");\n    }\n    void say(String content) {\n        System.out.println(content);\n    }\n    void lecturing() {\n        System.out.println(\"\u6211\u662f\u4e00\u540d\u8001\u5e08,\u6211\u7684\u5de5\u4f5c\u662f\u8bb2\u8bfe\");\n    }\n}\npublic class Demo {\n    public static void main(String[] args) {\n        People obj = new Teacher(); \/\/ \u5411\u4e0a\u8f6c\u578b\n        \/\/ obj.lecturing();          \/\/ \u4e0d\u80fd\u8c03\u7528\uff1a\u7236\u7c7b\u6ca1\u6709\n        \/\/ obj.say(\"\u6211\u7231\u5927\u5bb6\");      \/\/ \u4e0d\u80fd\u8c03\u7528\uff1a\u8fd9\u662f\u91cd\u8f7d\u51fa\u6765\u7684\u65b0\u7b7e\u540d\n        obj.say();                   \/\/ \u8c03\u7528\u5b50\u7c7b\u8986\u76d6\u540e\u7684 say()\n    }\n}<\/code><\/pre>\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;\">\u5199\u6cd5<\/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;\">\u5b9e\u9645\u5bf9\u8c61<\/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;\">\u7ed3\u679c<\/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;\">People obj = new Teacher()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Teacher<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5411\u4e0a\u8f6c\u578b\uff0c\u5b89\u5168\uff1bobj.say() \u8d70\u5b50\u7c7b<\/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;\">(Teacher) obj<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5fc5\u987b\u662f Teacher<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u624d\u80fd\u8c03 lecturing()\u3001say(String)<\/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;\">(Teacher) new People()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">People<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">ClassCastException<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\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\u5411\u4e0a\u8f6c\u578b\u770b\u7f16\u8bd1\u7c7b\u578b\u3001\u5411\u4e0b\u8f6c\u578b\u770b\u5b9e\u9645\u5bf9\u8c61\uff1b\u7236\u7c7b\u5f15\u7528\u53ea\u80fd\u8c03\u7236\u7c7b\u7b7e\u540d\uff0c\u8986\u76d6\u4e86\u7684\u65b9\u6cd5\u8fd0\u884c\u65f6\u4ecd\u8d70\u5b50\u7c7b\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u91cc\u8bf4\u7684\u5bf9\u8c61\u7c7b\u578b\u8f6c\u6362\uff0c\u53ea\u53d1\u751f\u5728\u6709\u7ee7\u627f\u5173\u7cfb\u7684\u7c7b\u578b\u4e4b\u95f4\u3002\u6ca1\u6709\u7ee7\u627f\u5173\u7cfb\u5374\u5f3a\u5236\u8f6c\uff0c\u8fd0\u884c\u65f6\u4f1a\u629b java.lang.Cl [&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,32,30],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/89"}],"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=89"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":2079,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions\/2079"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}