{"id":747,"date":"2022-10-07T11:58:19","date_gmt":"2022-10-07T03:58:19","guid":{"rendered":"https:\/\/qaqaq.top\/?p=747"},"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-%e8%bf%90%e7%ae%97%e7%ac%a6%e7%9a%84%e5%9b%9e%e9%a1%be%e3%80%81equals%e7%9a%84%e4%bd%bf%e7%94%a8%e3%80%81%e9%87%8d%e5%86%99equals%e3%80%81","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=747","title":{"rendered":"\u9762\u5411\u5bf9\u8c61(\u4e2d)-==\u8fd0\u7b97\u7b26\u7684\u56de\u987e\u3001equals()\u7684\u4f7f\u7528\u3001\u91cd\u5199equals()\u3001\u603b\u7ed3==\u4e0eequals()"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P295;\r\n\r\nimport java.util.Date;\r\n\r\n\/*\r\n * \r\n * \u9762\u8bd5\u9898\uff1a== \u548c equals() \u533a\u522b\r\n * \r\n * \u4e00\u3001\u56de\u987e == \u7684\u4f7f\u7528\uff1a\r\n * == \u8fd0\u7b97\u7b26\r\n * 1. \u53ef\u4ee5\u4f7f\u7528\u5728\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53d8\u91cf\u548c\u5f15\u7528\u6570\u636e\u7c7b\u578b\u53d8\u91cf\u4e2d\r\n * 2. \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\u3002\uff08\u4e0d\u4e00\u5b9a\u7c7b\u578b\u8981\u76f8\u540c\uff09\r\n * \t  \u5982\u679c\u6bd4\u8f83\u7684\u662f\u5f15\u7528\u6570\u636e\u7c7b\u578b\u53d8\u91cf\uff1a\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u503c\u662f\u5426\u76f8\u540c,\u5373\u4e24\u4e2a\u5f15\u7528\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f53\r\n * \u8865\u5145\uff1a== \u7b26\u53f7\u4f7f\u7528\u65f6\uff0c\u5fc5\u987b\u4fdd\u8bc1\u7b26\u53f7\u5de6\u53f3\u4e24\u8fb9\u7684\u53d8\u91cf\u7c7b\u578b\u4e00\u81f4\r\n * \r\n * \u4e8c\u3001equals() \u65b9\u6cd5\u7684\u4f7f\u7528\uff1a\r\n * 1. \u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u800c\u975e\u8fd0\u7b97\u7b26\r\n * 2. \u53ea\u80fd\u9002\u7528\u4e8e\u5f15\u7528\u6570\u636e\u7c7b\u578b\r\n * 3. Object\u7c7b\u4e2d equals() \u7684\u5b9a\u4e49\uff1a\r\n *\t\tpublic boolean equals(Object obj) {\r\n    \t\treturn (this == obj);\r\n  \t\t}\r\n * \t\t\u8bf4\u660e\uff1aObject \u7c7b\u4e2d\u5b9a\u4e49\u7684 equals() \u548c == \u7684\u4f5c\u7528\u662f\u76f8\u540c\u7684\uff1a\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u503c\u662f\u5426\u76f8\u540c,\u5373\u4e24\u4e2a\u5f15\u7528\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f53\r\n * \r\n * 4. \u50cfString\u3001Date\u3001File\u3001\u5305\u88c5\u7c7b\u7b49\u90fd\u91cd\u5199\u4e86 Object \u7c7b\u4e2d\u7684 equals() \u65b9\u6cd5\u3002\u91cd\u5199\u4ee5\u540e\uff0c\u6bd4\u8f83\u7684\u4e0d\u662f\r\n * \t\t\u4e24\u4e2a\u5f15\u7528\u7684\u5730\u5740\u662f\u5426\u76f8\u540c\uff0c\u800c\u662f\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\"\u5b9e\u4f53\u5185\u5bb9\"\u662f\u5426\u76f8\u540c\u3002\r\n * \r\n * 5. \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u7c7b\u5982\u679c\u4f7f\u7528 equals() \u7684\u8bdd\uff0c\u4e5f\u901a\u5e38\u662f\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\"\u5b9e\u4f53\u5185\u5bb9\"\u662f\u5426\u76f8\u540c\u3002\r\n * \t\t\u5c31\u9700\u8981\u5bf9Object\u7c7b\u4e2d\u7684 equals() \u8fdb\u884c\u91cd\u5199.\r\n * \t\t\u91cd\u5199\u7684\u539f\u5219\uff1a\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5b9e\u4f53\u5185\u5bb9\u662f\u5426\u76f8\u540c.\r\n * \r\n * \r\n *\/\r\npublic class EqualsTest {\r\n\tpublic static void main(String&#91;] args) {\r\n\t\t\r\n\t\t\/\/\u57fa\u672c\u6570\u636e\u7c7b\u578b\r\n\t\tint i = 10;\r\n\t\tint j = 10;\r\n\t\tdouble d = 10.0;\r\n\t\tSystem.out.println(i == j);\/\/true\r\n\t\tSystem.out.println(i == d);\/\/true\r\n\t\t\r\n\t\tboolean b = true;\r\n\/\/\t\tSystem.out.println(i == b);\r\n\t\t\r\n\t\tchar c = 10;\r\n\t\tSystem.out.println(i == c);\/\/true\r\n\t\t\r\n\t\tchar c1 = 'A';\r\n\t\tchar c2 = 65;\r\n\t\tSystem.out.println(c1 == c2);\/\/true\r\n\t\t\r\n\t\t\/\/\u5f15\u7528\u7c7b\u578b\r\n\t\tCustomer cust1 = new Customer(\"Tom\", 21);\r\n\t\tCustomer cust2 = new Customer(\"Tom\", 21);\r\n\t\tSystem.out.println(cust1 == cust2);\/\/false\r\n\t\t\r\n\t\tString str1 = new String(\"qaqaq\");\r\n\t\tString str2 = new String(\"qaqaq\");\r\n\t\tSystem.out.println(str1 == str2);\/\/false\r\n\t\tSystem.out.println(\"**********************************\");\r\n\t\t\r\n\t\tSystem.out.println(cust1.equals(cust2));\/\/false--->true\r\n\t\tSystem.out.println(str1.equals(str2));\/\/true\r\n\t\tDate date1 = new Date(32432525324L);\r\n\t\tDate date2 = new Date(32432525324L);\r\n\t\tSystem.out.println(date1.equals(date2));\/\/true\r\n\t\t\r\n\t\t\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.P295;\r\n\r\nimport java.util.Objects;\r\n\r\npublic class Customer {\r\n\r\n\tprivate String name;\r\n\tprivate int age;\r\n\r\n\tpublic Customer() {\r\n\t\tsuper();\r\n\t}\r\n\r\n\tpublic Customer(String name, int age) {\r\n\t\tsuper();\r\n\t\tthis.name = name;\r\n\t\tthis.age = age;\r\n\t}\r\n\r\n\tpublic String getName() {\r\n\t\treturn name;\r\n\t}\r\n\r\n\tpublic void setName(String name) {\r\n\t\tthis.name = name;\r\n\t}\r\n\r\n\tpublic int getAge() {\r\n\t\treturn age;\r\n\t}\r\n\r\n\tpublic void setAge(int age) {\r\n\t\tthis.age = age;\r\n\t}\r\n\r\n\t\/\/\u81ea\u52a8\u751f\u6210\u7684 equals()\r\n\t@Override\r\n\tpublic boolean equals(Object obj) {\r\n\t\tif (this == obj)\r\n\t\t\treturn true;\r\n\t\tif (obj == null)\r\n\t\t\treturn false;\r\n\t\tif (getClass() != obj.getClass())\r\n\t\t\treturn false;\r\n\t\tCustomer other = (Customer) obj;\r\n\t\treturn age == other.age &amp;&amp; Objects.equals(name, other.name);\r\n\t}\r\n\r\n\t\r\n\t\/\/\u91cd\u5199\u7684\u539f\u5219\uff1a\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5b9e\u4f53\u5185\u5bb9\uff08\u5373\uff1aname\u548cage\uff09\u662f\u5426\u76f8\u540c\r\n\t\/\/\u624b\u52a8\u5b9e\u73b0 equals() \u7684\u91cd\u5199\r\n\/\/\t@Override\r\n\/\/\tpublic boolean equals(Object obj) {\r\n\/\/\t\t\r\n\/\/\/\/\t\tSystem.out.println(\"Customer equals()...\");\r\n\/\/\t\tif (this == obj) {\r\n\/\/            return true;\r\n\/\/        }\r\n\/\/\t\t\r\n\/\/\t\tif(obj instanceof Customer) {\r\n\/\/\t\t\tCustomer cust = (Customer)obj;\r\n\/\/\t\t\t\/\/\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5c5e\u6027\u662f\u5426\u90fd\u76f8\u540c\r\n\/\/\/\/\t\t\tif(this.age == cust.age &amp;&amp; this.name.equals(cust.name)) {\r\n\/\/\/\/\t\t\t\treturn true;\r\n\/\/\/\/\t\t\t}else {\r\n\/\/\/\/\t\t\t\treturn false;\r\n\/\/\/\/\t\t\t}\r\n\/\/\t\t\t\r\n\/\/\t\t\t\/\/\u6216\r\n\/\/\t\t\treturn this.age == cust.age &amp;&amp; this.name.equals(cust.name);\r\n\/\/\t\t}else {\r\n\/\/\t\t\treturn false;\r\n\/\/\t\t}\r\n\/\/\t\t\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-747","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\/747"}],"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=747"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/747\/revisions"}],"predecessor-version":[{"id":748,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/747\/revisions\/748"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}