{"id":729,"date":"2022-10-06T22:56:42","date_gmt":"2022-10-06T14:56:42","guid":{"rendered":"https:\/\/qaqaq.top\/?p=729"},"modified":"2022-11-27T12:39:55","modified_gmt":"2022-11-27T04:39:55","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%ad-%e5%90%91%e4%b8%8b%e8%bd%ac%e5%9e%8b%e7%9a%84%e4%bd%bf%e7%94%a8%e3%80%81instanceof%e5%85%b3%e9%94%ae%e5%ad%97%e7%9a%84%e4%bd%bf%e7%94%a8%e3%80%81","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=729","title":{"rendered":"\u9762\u5411\u5bf9\u8c61(\u4e2d)-\u5411\u4e0b\u8f6c\u578b\u7684\u4f7f\u7528\u3001instanceof\u5173\u952e\u5b57\u7684\u4f7f\u7528\u3001\u5411\u4e0b\u8f6c\u578b\u7684\u51e0\u4e2a\u5e38\u89c1\u95ee\u9898"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P287;\r\n\r\npublic class Person {\r\n\t\r\n\tString name;\r\n\tint age;\r\n\t\r\n\tint id = 1001;\r\n\t\r\n\tpublic void eat() {\r\n\t\tSystem.out.println(\"\u4eba\uff1a\u5403\u996d\");\r\n\t}\r\n\t\r\n\tpublic void walk() {\r\n\t\tSystem.out.println(\"\u4eba\uff1a\u8d70\u8def\");\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P287;\r\n\r\npublic class Man extends Person {\r\n\r\n\tboolean isSmoking;\r\n\t\r\n\tint id = 1002;\r\n\t\r\n\tpublic void earnMoney() {\r\n\t\tSystem.out.println(\"\u7537\u4eba\u8d1f\u8d23\u6323\u94b1\u517b\u5bb6\");\r\n\t}\r\n\t\r\n\tpublic void eat() {\r\n\t\tSystem.out.println(\"\u7537\u4eba\u591a\u5403\u8089\uff0c\u957f\u808c\u8089\");\r\n\t}\r\n\t\r\n\tpublic void walk() {\r\n\t\tSystem.out.println(\"\u7537\u4eba\u9738\u6c14\u7684\u8d70\u8def\");\r\n\t}\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P287;\r\n\r\npublic class Woman extends Person {\r\n\r\n\tboolean isBeauty;\r\n\t\r\n\tpublic void goShopping() {\r\n\t\tSystem.out.println(\"\u5973\u4eba\u559c\u6b22\u8d2d\u7269\");\r\n\t}\r\n\t\r\n\tpublic void eat() {\r\n\t\tSystem.out.println(\"\u5973\u4eba\u5c11\u5403\uff0c\u4e3a\u4e86\u51cf\u80a5\");\r\n\t}\r\n\t\r\n\tpublic void walk() {\r\n\t\tSystem.out.println(\"\u5973\u4eba\u7a88\u7a95\u7684\u8d70\u8def\");\r\n\t}\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P287;\r\n\r\nimport java.util.Date;\r\n\r\n\/*\r\n * \u9762\u5411\u5bf9\u8c61\u7279\u5f81\u4e4b\u4e09\uff1a\u591a\u6001\u6027\r\n * \r\n * 1. \u7406\u89e3\u591a\u6001\u6027\uff1a\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u4e8b\u7269\u7684\u591a\u79cd\u5f62\u6001\u3002\r\n * 2. \u4f55\u4e3a\u591a\u6001\u6027\uff1a\r\n * \t\t\u5bf9\u8c61\u7684\u591a\u6001\u6027\uff1a\u7236\u7c7b\u7684\u5f15\u7528\u6307\u5411\u5b50\u7c7b\u7684\u5bf9\u8c61\uff08\u6216\u5b50\u7c7b\u7684\u5bf9\u8c61\u8d4b\u7ed9\u7236\u7c7b\u7684\u5f15\u7528\uff09\r\n * \r\n * 3. \u591a\u6001\u7684\u4f7f\u7528\uff1a\u865a\u62df\u65b9\u6cd5\u8c03\u7528\r\n * \t\t\u6709\u4e86\u5bf9\u8c61\u7684\u591a\u6001\u6027\u4ee5\u540e\uff0c\u6211\u4eec\u5728\u7f16\u8bd1\u671f\uff0c\u53ea\u80fd\u8c03\u7528\u7236\u7c7b\u4e2d\u58f0\u660e\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u8fd0\u884c\u671f\uff0c\u6211\u4eec\u5b9e\u9645\u6267\u884c\u7684\u662f\u5b50\u7c7b\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u3002\r\n * \t\t\u603b\u7ed3\uff1a\u7f16\u8bd1\uff0c\u770b\u5de6\u8fb9\uff1b\u8fd0\u884c\uff0c\u770b\u53f3\u8fb9\u3002\r\n * \r\n * 4. \u591a\u6001\u6027\u7684\u4f7f\u7528\u524d\u63d0\uff1a\u2460 \u7c7b\u7684\u7ee7\u627f\u5173\u7cfb \u2461 \u65b9\u6cd5\u7684\u91cd\u5199\r\n * \r\n * 5. \u5bf9\u8c61\u7684\u591a\u6001\u6027\uff0c\u53ea\u9002\u7528\u4e8e\u65b9\u6cd5\uff0c\u4e0d\u9002\u7528\u4e8e\u5c5e\u6027\uff08\u7f16\u8bd1\u548c\u8fd0\u884c\u90fd\u770b\u5de6\u8fb9\uff09\r\n * \r\n * ******************************************************************\r\n * \r\n * \r\n *\/\r\npublic class PersonTest {\r\n\t\r\n\tpublic static void main(String&#91;] args) {\r\n\t\t\r\n\t\tPerson p1 = new Person();\r\n\t\t\r\n\t\tp1.eat();\r\n\t\t\r\n\t\tMan man = new Man();\r\n\t\tman.eat();\r\n\t\tman.age = 25;\r\n\t\tman.earnMoney();\r\n\t\t\r\n\t\t\/\/*****************************************************\r\n\t\tSystem.out.println(\"***********************************\");\r\n\t\t\r\n\t\t\/\/\u5bf9\u8c61\u7684\u591a\u6001\u6027\uff1a\u7236\u7c7b\u7684\u5f15\u7528\u6307\u5411\u5b50\u7c7b\u7684\u5bf9\u8c61\r\n\t\tPerson p2 = new Man();\r\n\/\/\t\tPerson p3 = new Woman();\r\n\t\t\r\n\t\t\/\/\u591a\u6001\u7684\u4f7f\u7528\uff1a\u5f53\u8c03\u7528\u5b50\u7236\u7c7b\u540c\u540d\u540c\u53c2\u6570\u7684\u65b9\u6cd5\u65f6\uff0c\u5b9e\u9645\u6267\u884c\u7684\u662f\u5b50\u7c7b\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5 ---\u865a\u62df\u65b9\u6cd5\u8c03\u7528\r\n\t\tp2.eat();\r\n\t\tp2.walk();\r\n\t\t\r\n\/\/\t\tp2.earnMoney();\r\n\t\t\r\n\t\tSystem.out.println(p2.id);\/\/1001\r\n\t\t\r\n\t\tSystem.out.println(\"***********************************************************\");\r\n\t\t\/\/\u4e0d\u80fd\u8c03\u7528\u5b50\u7c7b\u6240\u7279\u6709\u7684\u65b9\u6cd5\u3001\u5c5e\u6027\uff1a\u7f16\u8bd1\u65f6\uff0cp2\u662fPerson\u7c7b\u578b\u3002\r\n\/\/\t\tp2.earnMoney();\r\n\t\tp2.name = \"Tom\";\r\n\/\/\t\tp2.isSmoking = true;\r\n\t\t\/\/\u6709\u4e86\u5bf9\u8c61\u7684\u591a\u6001\u6027\u4ee5\u540e\uff0c\u5185\u5b58\u4e2d\u5b9e\u9645\u4e0a\u662f\u52a0\u8f7d\u4e86\u5b50\u7c7b\u7279\u6709\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\uff0c\u4f46\u662f\u7531\u4e8e\u53d8\u91cf\u58f0\u660e\u4e3a\u7236\u7c7b\u7c7b\u578b\uff0c\u5bfc\u81f4\r\n\t\t\/\/\u7f16\u8bd1\u65f6\uff0c\u53ea\u80fd\u8c03\u7528\u7236\u7c7b\u4e2d\u58f0\u660e\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5b50\u7c7b\u7279\u6709\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u4e0d\u80fd\u8c03\u7528\u3002\r\n\t\t\r\n\t\t\/\/\u5982\u4f55\u624d\u80fd\u8c03\u7528\u5b50\u7c7b\u7279\u6709\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1f\r\n\t\t\/\/\u5411\u4e0b\u8f6c\u578b\uff1a\u4f7f\u7528\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u7b26\u3002\r\n\t\tMan m1 = (Man) p2;\r\n\t\tm1.earnMoney();\r\n\t\tm1.isSmoking = true;\r\n\t\t\r\n\t\t\/\/\u4f7f\u7528\u5f3a\u8f6c\u65f6\uff0c\u53ef\u80fd\u51fa\u73b0ClassCastException\u7684\u5f02\u5e38\u3002\r\n\/\/\t\tWoman w1 = (Woman)p2;\r\n\/\/\t\tw1.goShopping();\r\n\t\t\r\n\t\t\/*\r\n\t\t * instanceof:\u5173\u952e\u5b57\u7684\u4f7f\u7528\r\n\t\t * \r\n\t\t * a instanceof A:\u5224\u65ad\u5bf9\u8c61a\u662f\u5426\u662f\u7c7bA\u7684\u5b9e\u4f8b\u3002\u5982\u679c\u662f\uff0c\u8fd4\u56detrue\uff1a\u5982\u679c\u4e0d\u662f\uff0c\u8fd4\u56defalse\u3002\r\n\t\t * \r\n\t\t * \r\n\t\t * \u4f7f\u7528\u60c5\u5883\uff1a\u4e3a\u4e86\u907f\u514d\u5728\u5411\u4e0b\u8f6c\u578b\u65f6\u51fa\u73b0ClassCastException\u7684\u5f02\u5e38\uff0c\u6211\u4eec\u5728\u5411\u4e0b\u8f6c\u578b\u4e4b\u524d\uff0c\u5148\r\n\t\t * \u8fdb\u884cinstanceof\u7684\u5224\u65ad\uff0c\u4e00\u65e6\u8fd4\u56detrue\uff0c\u5c31\u8fdb\u884c\u5411\u4e0b\u8f6c\u578b\u3002\u5982\u679c\u8fd4\u56defalse\uff0c\u4e0d\u8fdb\u884c\u5411\u4e0b\u8f6c\u578b\u3002\r\n\t\t * \r\n\t\t * \u5982\u679ca instanceof A\u8fd4\u56detrue\uff0c\u5219a instanceof B\u4e5f\u8fd4\u56detrue.\r\n\t\t * \u5176\u4e2d\uff0c\u7c7bB\u662f\u7c7bA\u7684\u7236\u7c7b\r\n\t\t * \r\n\t\t * \r\n\t\t *\/\r\n\t\t\r\n\t\tif(p2 instanceof Woman) {\r\n\t\t\tWoman w1 = (Woman)p2;\r\n\t\t\tw1.goShopping();\r\n\t\t\tSystem.out.println(\"*******Woman*******\");\r\n\t\t}\r\n\r\n\t\tif(p2 instanceof Man) {\r\n\t\t\tMan m2 = (Man)p2;\r\n\t\t\tm2.earnMoney();\r\n\t\t\tSystem.out.println(\"*******Man*******\");\r\n\t\t}\r\n\t\t\r\n\t\tif(p2 instanceof Person) {\r\n\t\t\tSystem.out.println(\"*******Person*******\");\r\n\t\t}\r\n\t\t\r\n\t\tif(p2 instanceof Object) {\r\n\t\t\tSystem.out.println(\"*******Object*******\");\r\n\t\t}\r\n\t\t\r\n\t\t\/\/\u7ec3\u4e60\uff1a\r\n\t\t\/\/\u95ee\u9898\u4e00\uff1a\u7f16\u8bd1\u65f6\u901a\u8fc7\uff0c\u8fd0\u884c\u65f6\u4e0d\u901a\u8fc7\r\n\t\t\/\/\u4e3e\u4f8b\u4e00\uff1a\r\n\/\/\t\tPerson p3 = new Woman();\r\n\/\/\t\tMan m3 = (Man)p3;\r\n\t\t\/\/\u4e3e\u4f8b\u4e8c\uff1a\r\n\/\/\t\tPerson p4 = new Person();\r\n\/\/\t\tMan m4 = (Man)p4;\r\n\t\t\r\n\t\t\/\/\u95ee\u9898\u4e8c\uff1a\u7f16\u8bd1\u901a\u8fc7\uff0c\u8fd0\u884c\u65f6\u4e5f\u901a\u8fc7\r\n\/\/\t\tObject obj = new Woman();\r\n\/\/\t\tPerson p = (Person)obj;\r\n\t\t\r\n\t\t\/\/\u95ee\u9898\u4e09\uff1a\u7f16\u8bd1\u4e0d\u901a\u8fc7\r\n\/\/\t\tMan m5 = new Woman();\r\n\t\t\r\n\/\/\t\tString str = new Date();\r\n\/\/\t\t\r\n\/\/\t\tObject o = new Date();\r\n\/\/\t\tString str1 = (String)o;\r\n\t}\r\n\r\n}\r\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":[8],"tags":[46],"class_list":["post-729","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/729"}],"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=729"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":730,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/729\/revisions\/730"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}