{"id":768,"date":"2022-10-07T21:47:35","date_gmt":"2022-10-07T13:47:35","guid":{"rendered":"https:\/\/qaqaq.top\/?p=768"},"modified":"2022-12-19T20:08:25","modified_gmt":"2022-12-19T12:08:25","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%ad-%e5%a4%8d%e4%b9%a0-2","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=768","title":{"rendered":"\u9762\u5411\u5bf9\u8c61(\u4e2d)-\u590d\u4e60"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>1. \u5982\u4f55\u5b9e\u73b0\u5411\u4e0b\u8f6c\u578b\uff1f\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\u95ee\u9898\uff1f\u5982\u4f55\u89e3\u51b3\u6b64\u95ee\u9898\uff1f\n\nPerson p = new Man();\n\u4f7f\u7528\u5f3a\u8f6c\u7b26\uff1a()\nMan m = (Man)p;\n\n\u53ef\u80fdClassCastException\u5f02\u5e38\u3002\nif(p instanceof Man){\n\tMan m = (Man)p;\n}\n\n\u4f7f\u7528instanceof\u5728\u8fdb\u884c\u5411\u4e0b\u8f6c\u578b\u524d\u5224\u65ad\u3002\n\n2. == \u548c equals()\u6709\u4f55\u533a\u522b\uff1f\n\n==\uff1a\u53ef\u4ee5\u4f7f\u7528\u5728\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53d8\u91cf\u548c\u5f15\u7528\u6570\u636e\u7c7b\u578b\u53d8\u91cf\u4e2d\n\t\u5982\u679c\u6bd4\u8f83\u7684\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53d8\u91cf\uff0c\u6bd4\u8f83\u4e24\u4e2a\u53d8\u91cf\u4fdd\u5b58\u7684\u6570\u636e\u662f\u5426\u76f8\u7b49\n\t\u5982\u679c\u6bd4\u8f83\u7684\u662f\u5f15\u7528\u6570\u636e\u7c7b\u578b\u53d8\u91cf\uff0c\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u503c\u662f\u5426\u76f8\u7b49\n\t==\u7b26\u53f7\u4f7f\u7528\u65f6\uff0c\u5fc5\u987b\u4fdd\u8bc1\u7b26\u53f7\u5de6\u53f3\u4e24\u8fb9\u7684\u53d8\u91cf\u7c7b\u578b\u4e00\u81f4\n\n\nequals()\uff1a\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u800c\u975e\u8fd0\u7b97\u7b26\n\t\u53ea\u9002\u7528\u4e8e\u5f15\u7528\u6570\u636e\u7c7b\u578b\n\tObject\u7c7b\u4e2d\u5b9a\u4e49\u7684equals()\u548c==\u7684\u4f5c\u7528\u662f\u76f8\u540c\u7684\uff0c\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u503c\u662f\u5426\u76f8\u540c\n\t\u50cfString\u3001Date\u3001File\u3001\u5305\u88c5\u7c7b\u7b49\u90fd\u91cd\u5199\u4e86Object\u7c7b\u4e2d\u7684equals()\u65b9\u6cd5\uff0c\u91cd\u5199\u4ee5\u540e\uff0c\u6bd4\u8f83\u7684\u662f\u4e24\u4e2a\u5bf9\u8c61\u7684\"\u5b9e\u4f53\u5185\u5bb9\"\u662f\u5426\u76f8\u540c\n\t\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u7c7b\u5982\u679c\u4f7f\u7528equals()\u7684\u8bdd\uff0c\u4e5f\u662f\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\"\u5b9e\u4f53\u5185\u5bb9\"\u662f\u5426\u76f8\u540c\uff0c\u5c31\u9700\u8981\u5bf9Object\u7c7b\u4e2d\u7684equals()\u8fdb\u884c\u91cd\u5199\n\t\u91cd\u5199\u7684\u539f\u5219\uff1a\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5b9e\u4f53\u5185\u5bb9\u662f\u5426\u76f8\u540c\n\n3.class User{\n\tString name;\n\tint age;\n\t\t\/\/\u91cd\u5199\u5176equals()\u65b9\u6cd5\n\n\tpublic boolean equals(Object obj){\n\t\tif(obj == this){\n\t\t\treturn true;\n\t\t}\n\t\tif(obj instanceof User){\n\t\t\tUser u = (User)obj;\n\t\t\treturn this.age == u.age &amp;&amp; this.name.equals(u.name);\n\t\t}\n\n\t\treturn false;\n\t}\n}\n\n4. \u5199\u51fa8\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53ca\u5176\u5bf9\u5e94\u7684\u5305\u88c5\u7c7b\n\nbyte\tByte\nshort\tShort\nint\tInteger\nlong\tLong\nfloat\tFloat\ndouble\tDouble\nboolean\tBoolean\nchar\tCharacter\n\n5. \u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u5305\u88c5\u7c7b\u4e0eString\u4e09\u8005\u4e4b\u95f4\u5982\u4f55\u8f6c\u6362\n\n\u81ea\u52a8\u88c5\u7bb1\u3001\u81ea\u52a8\u62c6\u7bb1\n\nInteger i = 10;\n\n\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u5305\u88c5\u7c7b---&gt;String\uff1avalueOf(XXX xx)\nString---&gt;\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u5305\u88c5\u7c7b\uff1apaserXxx(String s)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P311;\n\npublic class User {\n\tString name;\n\tint age;\n\t\n\t@Override\n\tpublic boolean equals(Object obj) {\n\t\tif(this == obj) {\n\t\t\treturn true;\n\t\t}\n\t\t\n\t\tif(obj instanceof User) {\n\t\t\tUser user = (User)obj;\n\t\t\treturn this.name.equals(user.name) &amp;&amp; this.age == user.age;\n\t\t}\n\t\t\n\t\treturn false;\n\t}\n\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[46],"class_list":["post-768","post","type-post","status-publish","format-standard","hentry","category-java-","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/768"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=768"}],"version-history":[{"count":2,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/768\/revisions"}],"predecessor-version":[{"id":2656,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/768\/revisions\/2656"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}