{"id":203216,"date":"2025-05-22T10:53:03","date_gmt":"2025-05-22T02:53:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203216\/"},"modified":"2025-05-22T10:53:03","modified_gmt":"2025-05-22T02:53:03","slug":"java-%e4%b8%ad%e7%9a%84-setters-%e5%92%8c-record","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203216\/","title":{"rendered":"Java \u4e2d\u7684 Setters \u548c Record"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u4e2d\u7684 Setters \u548c Record<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aJava \u4e2d\u7684 Setters \u548c Record\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241102\/173051050467257ea8f4138.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>record<\/strong> \u662f\u4e00\u79cd\u7ed3\u6784\u4f53\uff0c\u5176\u7279\u70b9\u662f\u4e0d\u53ef\u53d8\uff0c\u5373 <strong>record<\/strong> \u7c7b\u578b\u7684\u5bf9\u8c61\u4e00\u65e6\u521b\u5efa\uff0c\u5176\u5c5e\u6027\u5c31\u65e0\u6cd5\u4fee\u6539\uff0c\u5373\u7b49\u4ef7\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u79f0\u4e4b\u4e3a<em>\u6570\u636e\u7c7b<\/em>\u6216dto\uff08\u6570\u636e\u4f20\u8f93\u5bf9\u8c61\uff09\u3002\u4f46\u662f\uff0c\u5982\u679c\u9700\u8981\u4f7f\u7528 <strong>setter<\/strong> \u65b9\u6cd5\u4fee\u6539\u67d0\u4e2a\u5c5e\u6027\uff0c\u5e76\u4e14\u8003\u8651\u5230\u8bb0\u5f55\u4e2d\u7684\u6bcf\u4e2a\u5c5e\u6027\u90fd\u662f final \u7c7b\u578b\uff0c\u90a3\u4e48\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<\/p>\n<p>\u4e3a\u4e86\u8bc1\u660e\u8fd9\u662f\u5426\u53ef\u884c\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u8bb0\u5f55 product\uff0c\u5b83\u5177\u6709\u540d\u79f0\u548c\u4ef7\u683c\u4e24\u4e2a\u5c5e\u6027\uff0c\u4ee5\u53ca\u5728 java \u4e2d\u5b9a\u4e49\u8bb0\u5f55\u65f6\u81ea\u52a8\u521b\u5efa\u7684\u76f8\u5e94\u65b9\u6cd5\uff1a<\/p>\n<pre>public record product(string name, double price) { }\n<\/pre>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u4f60\u521b\u5efa\u4e00\u4e2a product \u7c7b\u578b\u7684\u5bf9\u8c61\u5e76\u5c1d\u8bd5\u4fee\u6539 name \u5c5e\u6027\uff0c\u4f60\u4f1a\u53d1\u73b0\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u751a\u81f3\u6ca1\u6709 <strong>setter<\/strong> \u65b9\u6cd5\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<\/p>\n<pre>product p = new product(\"bread\", 1.0);\np.setname(\"water\"); \/\/ error: cannot resolve method 'setname' in 'product'\n<\/pre>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u77e5\u9053 <strong>record<\/strong> \u53ef\u4ee5\u6709\u5176\u4ed6\u65b9\u6cd5\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a setname(string name) \u65b9\u6cd5\u6765\u4fee\u6539 name \u5c5e\u6027\u5e76\u4e3a\u5176\u5206\u914d\u65b0\u503c\uff0c\u56e0\u4e3a\u7b54\u6848\u662f\u5426\u5b9a\u7684. \uff0c\u5b83\u4e0d\u50cf\u666e\u901a\u8bfe\u5802\u90a3\u6837\u5de5\u4f5c\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>public record product(string name, double price) {\n    \/\/ error: cannot asign a value to final variable 'name'\n    public void setname(string name) {\n        this.name = name;\n    }\n}\n<\/pre>\n<p>\u90a3\u4e48\u5982\u4f55\u5728java\u4e2d\u4fee\u6539<strong>\u8bb0\u5f55<\/strong>\u7684\u5c5e\u6027\u5462\uff1f\u7b54\u6848\u662f\uff0c\u5982\u679c <strong>set<\/strong> \u65b9\u6cd5\u8fd4\u56de <strong>record<\/strong> \u7684\u65b0\u5b9e\u4f8b\u53ca\u5176\u6bcf\u4e2a\u5c5e\u6027\uff0c\u5e76\u4e14\u663e\u7136\u5e26\u6709\u4fee\u6539\u540e\u7684\u5c5e\u6027\uff0c\u5219\u53ef\u4ee5\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u6709\u70b9\u4e4f\u5473\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e<strong>\u8bb0\u5f55<\/strong>\u5177\u6709\u7684\u5c5e\u6027\u6570\u91cf\u3002<\/p>\n<pre>public record product(string name, double price) {\n\n    public product setname(string name) {\n        return new product(name, this.price);\n    }\n\n    public product setprice(double price) {\n        return new product(this.name, price);\n    }\n\n}\n<\/pre>\n<p>\u8fd9\u6837\uff0c\u5f53\u8c03\u7528\u4efb\u4f55\u4e00\u4e2a<strong>setter<\/strong>\u65b9\u6cd5\u65f6\uff0c\u90fd\u4f1a\u83b7\u5f97\u4e00\u4e2a\u5177\u6709\u4fee\u6539\u5c5e\u6027\u7684product\u7c7b\u578b\u7684\u65b0\u5b9e\u4f8b\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>product p = new product(\"bread\", 1.0);\nproduct q = p.setname(\"milk\");\nproduct r = q.setprice(2.0);\n<\/pre>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u5bf9\u8c61 p\u3001q \u548c r\uff0c\u5176 get\u3001equals\u3001hashcode \u548c tostring \u65b9\u6cd5\u90fd\u53ef\u4ee5\u6b63\u5e38\u8c03\u7528\uff0c\u8003\u8651\u5230\u6ca1\u6709\u4e00\u4e2a\u5bf9\u8c61\u5f7c\u6b64\u76f8\u7b49\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u90fd\u4e0d\u540c\u3002<\/p>\n<pre>public class Main {\n    public static void main(String[] args) {\n        Product p = new Product(\"Bread\", 1.0);\n        Product q = p.setName(\"Milk\");\n        Product r = q.setPrice(2.0);\n\n        System.out.println(p); \/\/ Product[name=Bread, price=1.0]\n        System.out.println(q); \/\/ Product[name=Milk, price=1.0]\n        System.out.println(r); \/\/ Product[name=Milk, price=2.0]\n\n        System.out.println(p.equals(q)); \/\/ false\n        System.out.println(q.equals(r)); \/\/ false\n        System.out.println(r.equals(p)); \/\/ false\n    }\n}\n<\/pre>\n<p>\u6b64\u65f6\uff0c\u8003\u8651\u5230<strong>\u8bb0\u5f55<\/strong>\u88ab\u8bbe\u8ba1\u4e3a\u4e00\u79cd\u5141\u8bb8\u4ee5\u7b80\u5355\u7684\u65b9\u5f0f\u5b58\u50a8\u548c\u4f20\u8f93\u4fe1\u606f\u7684\u7ed3\u6784\uff0c\u8003\u8651\u8fd9\u79cd\u65b9\u6cd5\u662f\u5426\u9002\u5408\u8981\u89e3\u51b3\u7684\u95ee\u9898\u975e\u5e38\u91cd\u8981\u3002\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u65b9\u5f0f\u5e76\u4e14\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u6216\u8005\u5982\u679c\u9700\u8981\u5177\u6709\u66f4\u5927\u7075\u6d3b\u6027\u7684\u7ed3\u6784\uff0c\u90a3\u4e48\u5e94\u8be5\u4f7f\u7528 <strong>class<\/strong> \u3002\u6709\u5173\u4f55\u65f6\u4f7f\u7528<strong>\u8bb0\u5f55<\/strong>\u6216<strong>\u7c7b<\/strong>\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u60a8\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u5e16\u5b50\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aJava \u4e2d\u7684 Setters \u548c Record\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u4e2d\u7684 Setters &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203216","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203216"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203216\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}