{"id":36790,"date":"2024-11-26T12:13:48","date_gmt":"2024-11-26T04:13:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36790\/"},"modified":"2024-11-26T12:13:48","modified_gmt":"2024-11-26T04:13:48","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%bc%ba%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36790\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u5f3a\u8f6c"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u901a\u8fc7\u5f3a\u5236\u8f6c\u6362\u8fd0\u7b97\u7b26\u5c06\u5bf9\u8c61\u6570\u7ec4\u5f3a\u5236\u8f6c\u6362\u4e3a\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u524d\u63d0\u662f\u6570\u7ec4\u5143\u7d20\u90fd\u662f\u6240\u671f\u671b\u7684\u7c7b\u578b\uff1a\u786e\u5b9a\u8981\u8f6c\u6362\u7684\u7c7b\u578b\u3002\u4f7f\u7528 (\u7c7b\u578b\u540d\u79f0[]) \u8fd0\u7b97\u7b26\u5f3a\u5236\u8f6c\u6362\u6570\u7ec4\u3002\u5c06\u5f3a\u5236\u8f6c\u6362\u540e\u7684\u6570\u7ec4\u8d4b\u503c\u7ed9\u65b0\u53d8\u91cf\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111218453063487.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u5f3a\u8f6c\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u5f3a\u8f6c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5f3a\u5236\u8f6c\u6362 Java \u6570\u7ec4<\/strong><\/p>\n<p>\u5f53\u9700\u8981\u5c06\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4\u5f3a\u8f6c\u4e3a\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f3a\u5236\u8f6c\u6362\u65b9\u6cd5\u3002\u5f3a\u5236\u8f6c\u6362\u53ef\u80fd\u4f1a\u629b\u51fa ClassCastException \u5f02\u5e38\uff0c\u5982\u679c\u5bf9\u8c61\u4e0d\u662f\u6240\u671f\u671b\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5f3a\u5236\u8f6c\u6362 Java \u6570\u7ec4\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u76ee\u6807\u7c7b\u578b\uff1a<\/strong>\u786e\u5b9a\u8981\u5c06\u5bf9\u8c61\u6570\u7ec4\u5f3a\u8f6c\u4e3a\u7684\u7279\u5b9a\u7c7b\u578b\u3002<\/li>\n<li> <strong>\u4f7f\u7528\u5f3a\u5236\u8f6c\u6362\u8fd0\u7b97\u7b26\uff1a<\/strong>\u4f7f\u7528 (\u7c7b\u578b\u540d\u79f0[]) \u8fd0\u7b97\u7b26\u5f3a\u5236\u8f6c\u6362\u6570\u7ec4\u3002<\/li>\n<li> <strong>\u8d4b\u503c\u7ed9\u65b0\u53d8\u91cf\uff1a<\/strong>\u5c06\u5f3a\u5236\u8f6c\u6362\u540e\u7684\u6570\u7ec4\u8d4b\u503c\u7ed9\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u4ee5\u83b7\u53d6\u5f3a\u8f6c\u540e\u7684\u7c7b\u578b\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a Object \u6570\u7ec4\nObject[] objArray = {\"Apple\", \"Orange\", \"Banana\"};\n\n\/\/ \u5f3a\u5236\u8f6c\u6362\u4e3a String \u6570\u7ec4\nString[] strArray = (String[]) objArray;\n\n\/\/ \u4f7f\u7528\u5f3a\u8f6c\u540e\u7684\u6570\u7ec4\nSystem.out.println(strArray[0]); \/\/ \u8f93\u51fa\uff1aApple<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u5bf9\u8c61\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u662f\u6240\u671f\u671b\u7684\u76ee\u6807\u7c7b\u578b\uff0c\u5426\u5219\u4f1a\u629b\u51fa ClassCastException \u5f02\u5e38\u3002<\/li>\n<li>\u5f3a\u5236\u8f6c\u6362\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\uff0c\u5b83\u53ea\u662f\u521b\u5efa\u4e86\u4e00\u4e2a\u5177\u6709\u76ee\u6807\u7c7b\u578b\u7684\u89c6\u56fe\u3002<\/li>\n<li>\u5982\u679c\u76ee\u6807\u7c7b\u578b\u4e0d\u662f\u539f\u59cb\u6570\u7ec4\u7c7b\u578b\u7684\u5b50\u7c7b\u578b\uff0c\u5219\u5f3a\u5236\u8f6c\u6362\u5c06\u5931\u8d25\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u5f3a\u8f6c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u901a\u8fc7\u5f3a\u5236\u8f6c\u6362\u8fd0\u7b97\u7b26\u5c06\u5bf9\u8c61\u6570\u7ec4\u5f3a\u5236\u8f6c\u6362\u4e3a\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u524d\u63d0\u662f\u6570\u7ec4\u5143\u7d20\u90fd\u662f\u6240\u671f\u671b\u7684\u7c7b\u578b\uff1a\u786e\u5b9a\u8981\u8f6c\u6362\u7684\u7c7b\u578b\u3002\u4f7f\u7528 (\u7c7b\u578b\u540d\u79f0[]) \u8fd0\u7b97\u7b26\u5f3a\u5236\u8f6c\u6362\u6570\u7ec4\u3002\u5c06\u5f3a\u5236\u8f6c\u6362\u540e\u7684\u6570\u7ec4\u8d4b\u503c\u7ed9\u65b0\u53d8\u91cf\u3002 \u5982\u4f55\u5f3a\u5236\u8f6c\u6362 Java \u6570\u7ec4 \u5f53\u9700\u8981\u5c06\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4\u5f3a\u8f6c\u4e3a\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f3a\u5236\u8f6c\u6362\u65b9\u6cd5\u3002\u5f3a\u5236\u8f6c\u6362\u53ef\u80fd\u4f1a\u629b\u51fa ClassCastException \u5f02\u5e38\uff0c\u5982\u679c\u5bf9\u8c61\u4e0d\u662f\u6240\u671f\u671b\u7684\u7c7b\u578b\u3002 \u5f3a\u5236\u8f6c\u6362 Java \u6570\u7ec4\u7684\u6b65\u9aa4\uff1a \u786e\u5b9a\u76ee\u6807\u7c7b\u578b\uff1a\u786e\u5b9a\u8981\u5c06\u5bf9\u8c61\u6570\u7ec4\u5f3a\u8f6c\u4e3a\u7684\u7279\u5b9a\u7c7b\u578b\u3002 \u4f7f\u7528\u5f3a\u5236\u8f6c\u6362\u8fd0\u7b97\u7b26\uff1a\u4f7f\u7528 (\u7c7b\u578b\u540d\u79f0[]) \u8fd0\u7b97\u7b26\u5f3a\u5236\u8f6c\u6362\u6570\u7ec4\u3002 \u8d4b\u503c\u7ed9\u65b0\u53d8\u91cf\uff1a\u5c06\u5f3a\u5236\u8f6c\u6362\u540e\u7684\u6570\u7ec4\u8d4b\u503c\u7ed9\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u4ee5\u83b7\u53d6\u5f3a\u8f6c\u540e\u7684\u7c7b\u578b\u3002 \u793a\u4f8b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u521b\u5efa\u4e00\u4e2a Object \u6570\u7ec4 Object[] objArray = {&#8220;Apple&#8221;, &#8220;Orange&#8221;, &#8220;Banana&#8221;}; \/\/ \u5f3a\u5236\u8f6c\u6362\u4e3a String \u6570\u7ec4 String[] strArray = (String[]) objArray; \/\/ \u4f7f\u7528\u5f3a\u8f6c\u540e\u7684\u6570\u7ec4 System.out.println(strArray[0]); \/\/ \u8f93\u51fa\uff1aApple \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\u4e8b\u9879\uff1a \u786e\u4fdd\u5bf9\u8c61\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u662f\u6240\u671f\u671b\u7684\u76ee\u6807\u7c7b\u578b\uff0c\u5426\u5219\u4f1a\u629b\u51fa ClassCastException \u5f02\u5e38\u3002 \u5f3a\u5236\u8f6c\u6362\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\uff0c\u5b83\u53ea\u662f\u521b\u5efa\u4e86\u4e00\u4e2a\u5177\u6709\u76ee\u6807\u7c7b\u578b\u7684\u89c6\u56fe\u3002 \u5982\u679c\u76ee\u6807\u7c7b\u578b\u4e0d\u662f\u539f\u59cb\u6570\u7ec4\u7c7b\u578b\u7684\u5b50\u7c7b\u578b\uff0c\u5219\u5f3a\u5236\u8f6c\u6362\u5c06\u5931\u8d25\u3002 \u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u5f3a\u8f6c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-36790","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=36790"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36790\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}