{"id":87,"date":"2015-03-30T11:45:51","date_gmt":"2015-03-30T03:45:51","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=87"},"modified":"2026-08-18T12:09:25","modified_gmt":"2026-08-18T04:09:25","slug":"java-instanceof-%e8%bf%90%e7%ae%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=87","title":{"rendered":"Java instanceof \u8fd0\u7b97\u7b26"},"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;\">\u591a\u6001\u5e26\u6765\u4e00\u4e2a\u5b9e\u9645\u95ee\u9898\uff1a\u53d8\u91cf\u58f0\u660e\u7c7b\u578b\u662f\u7236\u7c7b\uff0c\u600e\u4e48\u5224\u65ad\u5b83<strong style=\"color: #4870ac;\">\u5b9e\u9645\u5f15\u7528<\/strong>\u7684\u5bf9\u8c61\u662f\u54ea\u4e00\u79cd\uff1fJava \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;\">instanceof<\/code> \u8fd0\u7b97\u7b26\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><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;\">obj instanceof Type<\/code> \u770b\u7684\u662f\u5bf9\u8c61\u7684\u771f\u5b9e\u7c7b\u578b\uff0c\u4e0d\u662f\u53d8\u91cf\u7684\u58f0\u660e\u7c7b\u578b\u3002\u771f\u5b9e\u5bf9\u8c61\u662f Type \u672c\u8eab\uff0c\u6216 Type \u7684\u5b50\u7c7b\uff0c\u7ed3\u679c\u4e3a true\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\u7ee7\u627f\u94fe<\/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}\nclass Teacher extends People {\n}\nclass President extends Teacher {\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u94fe\u662f Object \u2192 People \u2192 Teacher \u2192 President\u3002\u8d8a\u5f80\u4e0b\u8d8a\u5177\u4f53\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\u540c\u4e00\u53d8\u91cf\uff0c\u4e24\u79cd\u5b9e\u9645\u5bf9\u8c61<\/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;\">public class Demo {\n    public static void main(String[] args) {\n        People obj = new People();\n        if (obj instanceof Object) {\n            System.out.println(\"\u6211\u662f\u4e00\u4e2a\u5bf9\u8c61\");\n        }\n        if (obj instanceof People) {\n            System.out.println(\"\u6211\u662f\u4eba\u7c7b\");\n        }\n        if (obj instanceof Teacher) {\n            System.out.println(\"\u6211\u662f\u4e00\u540d\u6559\u5e08\");\n        }\n        if (obj instanceof President) {\n            System.out.println(\"\u6211\u662f\u6821\u957f\");\n        }\n        System.out.println(\"-----------\");\n\n        obj = new Teacher();\n        if (obj instanceof Object) {\n            System.out.println(\"\u6211\u662f\u4e00\u4e2a\u5bf9\u8c61\");\n        }\n        if (obj instanceof People) {\n            System.out.println(\"\u6211\u662f\u4eba\u7c7b\");\n        }\n        if (obj instanceof Teacher) {\n            System.out.println(\"\u6211\u662f\u4e00\u540d\u6559\u5e08\");\n        }\n        if (obj instanceof President) {\n            System.out.println(\"\u6211\u662f\u6821\u957f\");\n        }\n    }\n}\n\/\/ \u8fd0\u884c\u7ed3\u679c\uff1a\n\/\/ \u6211\u662f\u4e00\u4e2a\u5bf9\u8c61\n\/\/ \u6211\u662f\u4eba\u7c7b\n\/\/ -----------\n\/\/ \u6211\u662f\u4e00\u4e2a\u5bf9\u8c61\n\/\/ \u6211\u662f\u4eba\u7c7b\n\/\/ \u6211\u662f\u4e00\u540d\u6559\u5e08<\/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;\">\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;\">instanceof Object \/ People<\/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;\">instanceof Teacher \/ President<\/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;\">new People()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">true \/ true<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">false \/ false<\/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;\">new Teacher()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">true \/ true<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">true \/ false<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin: 1em 0px;\">Teacher \u662f People \u7684\u5b50\u7c7b\uff0c\u6240\u4ee5 <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 Teacher() instanceof People<\/code> \u4e3a true\uff1bPeople \u4e0d\u662f Teacher\uff0c\u6240\u4ee5\u7b2c\u4e00\u6bb5\u4e0d\u4f1a\u6253\u5370\u300c\u6211\u662f\u4e00\u540d\u6559\u5e08\u300d\u3002\u5411\u4e0b\u95ee\u66f4\u5177\u4f53\u7684\u7c7b\u578b\uff0c\u53ea\u6709\u5b9e\u9645\u5bf9\u8c61\u591f\u300c\u5177\u4f53\u300d\u624d\u4e3a true\u3002<\/p>\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\uff1ainstanceof \u95ee\u7684\u662f\u5806\u4e0a\u90a3\u4e2a\u5bf9\u8c61\u662f\u4e0d\u662f\u67d0\u7c7b\u578b\u6216\u5176\u5b50\u7c7b\uff0c\u4e0d\u95ee\u53d8\u91cf\u5199\u6210\u4e86\u4ec0\u4e48\u7c7b\u578b\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u6001\u5e26\u6765\u4e00\u4e2a\u5b9e\u9645\u95ee\u9898\uff1a\u53d8\u91cf\u58f0\u660e\u7c7b\u578b\u662f\u7236\u7c7b\uff0c\u600e\u4e48\u5224\u65ad\u5b83\u5b9e\u9645\u5f15\u7528\u7684\u5bf9\u8c61\u662f\u54ea\u4e00\u79cd\uff1fJava \u7528 instanceof [&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],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/87"}],"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=87"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":2081,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions\/2081"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}