{"id":10125,"date":"2023-11-28T21:27:03","date_gmt":"2023-11-28T13:27:03","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10125"},"modified":"2023-12-07T10:52:52","modified_gmt":"2023-12-07T02:52:52","slug":"java-zhong-ru-he-pan-duan-liang-ge-lei-shi-fou-xia","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhong-ru-he-pan-duan-liang-ge-lei-shi-fou-xia\/","title":{"rendered":"Java\u4e2d\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u7c7b\u662f\u5426\u76f8\u7b49\uff1f"},"content":{"rendered":"<p>\u5728Java\u4e2d\uff0c\u5224\u65ad\u4e24\u4e2a\u7c7b\u662f\u5426\u76f8\u7b49\u901a\u5e38\u6d89\u53ca\u4e24\u4e2a\u65b9\u9762\u7684\u6bd4\u8f83\uff1a\u5f15\u7528\u76f8\u7b49\u6027\u548c\u5bf9\u8c61\u5185\u5bb9\u76f8\u7b49\u6027\u3002<\/p>\n<h3><a id=\"%E5%BC%95%E7%94%A8%E7%9B%B8%E7%AD%89%E6%80%A7%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5f15\u7528\u76f8\u7b49\u6027\uff1a<\/h3>\n<p>\u5f15\u7528\u76f8\u7b49\u6027\u662f\u6307\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>==<\/code> \u64cd\u4f5c\u7b26\u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u4e24\u4e2a\u53d8\u91cf\u5f15\u7528\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5219\u5b83\u4eec\u662f\u5f15\u7528\u76f8\u7b49\u7684\u3002<\/p>\n<pre><code class=\"language-java\">Object obj1 = new Object();\nObject obj2 = obj1;\n\nif (obj1 == obj2) {\n    System.out.println(&quot;\u5f15\u7528\u76f8\u7b49&quot;);\n} else {\n    System.out.println(&quot;\u5f15\u7528\u4e0d\u76f8\u7b49&quot;);\n}\n<\/code><\/pre>\n<h3><a id=\"%E5%AF%B9%E8%B1%A1%E5%86%85%E5%AE%B9%E7%9B%B8%E7%AD%89%E6%80%A7%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5bf9\u8c61\u5185\u5bb9\u76f8\u7b49\u6027\uff1a<\/h3>\n<p>\u5bf9\u8c61\u5185\u5bb9\u76f8\u7b49\u6027\u662f\u6307\u4e24\u4e2a\u5bf9\u8c61\u7684\u5185\u90e8\u72b6\u6001\u662f\u5426\u76f8\u7b49\u3002\u901a\u5e38\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u81ea\u5b9a\u4e49\u7c7b\u7684 <code>equals<\/code> \u65b9\u6cd5\u6765\u6bd4\u8f83\u5bf9\u8c61\u7684\u5185\u5bb9\u662f\u5426\u76f8\u7b49\u3002\u5728Java\u4e2d\uff0c<code>equals<\/code> \u65b9\u6cd5\u7ee7\u627f\u81ea <code>Object<\/code> \u7c7b\uff0c\u4f46\u9ed8\u8ba4\u5b9e\u73b0\u662f\u6bd4\u8f83\u5bf9\u8c61\u7684\u5f15\u7528\u662f\u5426\u76f8\u7b49\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u91cd\u5199\u3002<\/p>\n<pre><code class=\"language-java\">public class MyClass {\n    private int value;\n\n    \/\/ \u6784\u9020\u65b9\u6cd5\u548c\u5176\u4ed6\u4e1a\u52a1\u903b\u8f91\n\n    @Override\n    public boolean equals(Object o) {\n        if (this == o) return true;\n        if (o == null || getClass() != o.getClass()) return false;\n        MyClass myClass = (MyClass) o;\n        return value == myClass.value;\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>equals<\/code> \u65b9\u6cd5\u88ab\u91cd\u5199\uff0c\u6bd4\u8f83\u4e86 <code>MyClass<\/code> \u7c7b\u7684 <code>value<\/code> \u5c5e\u6027\u3002<\/p>\n<h3><a id=\"%E4%BD%BF%E7%94%A8objects%E7%B1%BB%E7%9A%84equals%E6%96%B9%E6%B3%95%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528Objects\u7c7b\u7684equals\u65b9\u6cd5\uff1a<\/h3>\n<p>\u4e3a\u4e86\u907f\u514d\u5728\u81ea\u5b9a\u4e49\u7684 <code>equals<\/code> \u65b9\u6cd5\u4e2d\u5904\u7406 <code>null<\/code> \u5f15\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Objects<\/code> \u7c7b\u7684 <code>equals<\/code> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5728Java 7\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u63d0\u4f9b\u3002<\/p>\n<pre><code class=\"language-java\">import java.util.Objects;\n\npublic class MyClass {\n    private int value;\n\n    \/\/ \u6784\u9020\u65b9\u6cd5\u548c\u5176\u4ed6\u4e1a\u52a1\u903b\u8f91\n\n    @Override\n    public boolean equals(Object o) {\n        if (this == o) return true;\n        if (o == null || getClass() != o.getClass()) return false;\n        MyClass myClass = (MyClass) o;\n        return value == myClass.value;\n    }\n\n    @Override\n    public int hashCode() {\n        return Objects.hash(value);\n    }\n}\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u662f\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5177\u4f53\u7684 <code>equals<\/code> \u65b9\u6cd5\u7684\u5b9e\u73b0\u9700\u8981\u6839\u636e\u7c7b\u7684\u5c5e\u6027\u548c\u4e1a\u52a1\u903b\u8f91\u6765\u786e\u5b9a\u3002\u5728\u5b9e\u73b0 <code>equals<\/code> \u65b9\u6cd5\u65f6\uff0c\u8fd8\u5e94\u8be5\u540c\u65f6\u91cd\u5199 <code>hashCode<\/code> \u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u5bf9\u8c61\u5728\u4f7f\u7528\u6563\u5217\u6570\u636e\u7ed3\u6784\u65f6\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Java\u4e2d\uff0c\u5224\u65ad\u4e24\u4e2a\u7c7b\u662f\u5426\u76f8\u7b49\u901a\u5e38\u6d89\u53ca\u4e24\u4e2a\u65b9\u9762\u7684\u6bd4\u8f83\uff1a\u5f15\u7528\u76f8\u7b49\u6027\u548c\u5bf9\u8c61\u5185\u5bb9\u76f8\u7b49\u6027\u3002 \u5f15\u7528\u76f8\u7b49\u6027\uff1a \u5f15\u7528\u76f8\u7b49\u6027 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-10125","post","type-post","status-publish","format-standard","hentry","category-jvm"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=10125"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10125\/revisions"}],"predecessor-version":[{"id":16803,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10125\/revisions\/16803"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10125"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}