{"id":43,"date":"2021-04-23T11:39:33","date_gmt":"2021-04-23T03:39:33","guid":{"rendered":"https:\/\/blog.king011.com\/?p=43"},"modified":"2021-04-27T14:02:58","modified_gmt":"2021-04-27T06:02:58","slug":"%e5%af%a6%e7%8f%be%e5%84%aa%e9%9b%85%e7%9a%84-new-%e5%87%bd%e6%95%b8%e5%89%b5%e5%bb%ba%e5%b0%8d%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/blog.king011.com\/?p=43","title":{"rendered":"\u5be6\u73fe\u512a\u96c5\u7684 New \u51fd\u6578\u5275\u5efa\u5c0d\u8c61"},"content":{"rendered":"\n<p>golang \u6c92\u6709\u69cb\u9020\u51fd\u6578\uff0c\u6240\u4ee5\u901a\u5e38\u7684\u505a\u6cd5\u662f\u7232\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u63d0\u4f9b\u4e00\u500b New \u51fd\u6578\u4f86\u5275\u5efa\u5c0d\u8c61\uff0c\u6bd4\u5982\u6709\u4e00\u500b\u81ea\u5b9a\u7fa9\u7684 Cat \u985e\u578b\uff0c\u5c31\u9700\u8981\u5275\u5efa\u4e00\u500b NewCat \u51fd\u6578\u4f86\u5b8c\u6210 Cat \u7684\u5167\u5b58\u5206\u914d\u4ee5\u53ca\u4e00\u4e9b\u521d\u59cb\u5316\u8a2d\u5b9a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Cat struct {\n\tname string\n}\n\nfunc NewCat(name string) *Cat {\n\treturn &amp;Cat{\n\t\tname: name,\n\t}\n}<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0 NewCat \u662f\u4e00\u500b New \u51fd\u6578\uff0c\u4f46\u4e0d\u662f\u4e00\u500b\u512a\u96c5\u7684 New \u51fd\u6578\uff0c\u56e0\u7232\u9019\u7a2e\u5beb\u6cd5\u5b58\u5728\u660e\u986f\u7684\u7f3a\u9677\uff0c\u5982\u679c\u6211\u5011\u7684 Cat \u9084\u9700\u8981\u4e00\u500b color \u5c6c\u6027\u6307\u5b9a\u5979\u7684\u984f\u8272\uff0c\u4f60\u4e0d\u5f97\u4e0d\u5275\u5efa\u4e00\u500b\u985e\u4f3c NewCat2(name string,color Color) \u7684\u51fd\u6578\uff0c\u4f46\u9019\u9084\u6c92\u5b8c\u53c8\u904e\u4e86\u4e00\u6bb5\u6642\u9593 Cat \u8b8a\u5f97\u66f4\u52a0\u5f37\u5927\u9084\u9700\u8981\u4e00\u500b Age \u5c6c\u6027\u4f60\u53c8\u8981\u5275\u5efa\u4e00\u500b NewCat3 \u51fd\u6578\u3002\u986f\u7136\u6211\u5011\u9700\u8981\u4e00\u500b\u512a\u96c5\u7684\u65b9\u6848\u4f86\u5be6\u73fe New\uff0c\u9019\u500b New \u51fd\u6578\u9700\u8981\u80fd\u5920\u65b9\u4fbf\u64f4\u5145\u4e26\u4e14\u4e0d\u6703\u5c0d\u5df2\u6709\u4ee3\u78bc\u9020\u6210\u50b7\u5bb3\u3002\u672c\u6587\u5c31\u662f\u63a2\u8a0e\u5982\u4f55\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u6587\u7ae0\u7d50\u5c3e\u7d66\u51fa\u4e86\u6700\u7d42\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u4e0d\u904e\u6211\u5011\u5148\u4f86\u770b\u770b\u5176\u5b83\u8a9e\u8a00\u4ee5\u53ca\u4e00\u4e9b\u4e0d\u592a\u512a\u96c5\u7684\u65b9\u6848\u662f\u600e\u9ebc\u89e3\u6c7a\u554f\u984c\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c3\u6578\u985e\u578b<\/h2>\n\n\n\n<p>\u9019\u662f\u6700\u539f\u59cb\u7684 c \u8a9e\u8a00\u7684\u505a\u6cd5\uff0c\u4f46\u5f88\u6709\u6548\u6240\u4ee5\u5176\u5b83\u8a9e\u8a00\u88cf\u9762\u4e5f\u6709\u985e\u4f3c\u7684\u4ee3\u78bc\uff0c\u5176\u539f\u7406\u662f\u5b9a\u7fa9\u4e00\u500b Options \u985e\u578b\u5305\u542b\u4e86\u6240\u6709\u7684\u5c6c\u6027\uff0c\u6bd4\u5982\u6211\u5011\u7232 Cat \u5b9a\u7fa9\u4e00\u500b CatOptions \u88cf\u9762\u5305\u542b\u4e86\u6240\u6709\u7684\u521d\u59cb\u5316\u53c3\u6578\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Cat struct {\n\tname string\n}\ntype CatOptions struct {\n\tName string\n}\n\nfunc NewCat(opts *CatOptions) *Cat {\n\treturn &amp;Cat{\n\t\tname: opts.Name,\n\t}\n}<\/code><\/pre>\n\n\n\n<p>\u73fe\u5728\u4f60\u4e0d\u9700\u8981\u5275\u5efa NewCat2 NewCat3 &#8230; \u7b49\u4e00\u7cfb\u5217\u51fd\u6578\u4e86\uff0c\u7528\u6236\u6c38\u9060\u53ea\u9700\u8981\u8abf\u7528 NewCat \u4fbf\u80fd\u5920\u5f97\u5230\u4e00\u500b\u65b0\u7684 Cat \uff0c\u4e26\u4e14\u7576\u64f4\u5145 Cat \u6642 \u4f60\u53ea\u9700\u8981\u4fee\u6539 CatOptions \u7684\u5b9a\u7fa9 \u548c NewCat \u7684\u5be6\u73fe\u5373\u53ef\uff0c\u4f46\u9019\u5c0d\u8abf\u7528\u8005\u4f86\u8aaa\u662f\u512a\u96c5\u7684\u56e0\u7232\u4e00\u5207\u7d30\u7bc0\u90fd\u88ab\u4f60\u96b1\u85cf\u8d77\u4f86\u4e86\uff0c\u4f46\u5c0d\u5be6\u73fe\u8005\u4f86\u8aaa\u4e26\u4e0d\u7b97\u512a\u96c5\u56e0\u7232\u4f60\u9700\u8981\u4e0d\u65b7\u7684\u4fee\u6539 Cat \u5be6\u73fe\u7232\u65b0\u7684\u5c6c\u6027\u8a2d\u7f6e\u4e0a\u5408\u9069\u7684\u9ed8\u8a8d\u503c\uff0c\u4ee5\u4fbf\u8abf\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u820a\u4ee3\u78bc\u8abf\u7528\u4f60\u7684\u65b0\u7684 Cat<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Cat struct {\n\tname  string\n\tcolor uint8\n}\ntype CatOptions struct {\n\tName  string\n\tColor uint8\n}\n\nfunc NewCat(opts *CatOptions) *Cat {\n\tcolor := opts.Color\n\tif color == 0 {\n\t\tcolor = 1\n\t}\n\treturn &amp;Cat{\n\t\tname:  opts.Name,\n\t\tcolor: color,\n\t}\n}<\/code><\/pre>\n\n\n\n<p>\u6b64\u5916\u9019\u7a2e\u5be6\u73fe\u65b9\u6848\u6700\u5927\u7684\u554f\u984c\u9084\u5728\u65bc\u7576 Options \u5305\u542b\u592a\u591a\u5b57\u6bb5\u6642\u53ef\u80fd\u6703\u5687\u8dd1\u8abf\u7528\u8005\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u51fd\u6578\u91cd\u8f09<\/h2>\n\n\n\n<p>\u5c0d\u65bc\u4e00\u4e9b\u652f\u6301\u91cd\u8f09\u51fd\u6578\u7684\u8a9e\u8a00\uff0c\u63d0\u4f9b\u7684\u89e3\u6c7a\u65b9\u6848\u5c31\u662f\u91cd\u8f09\u69cb\u9020\u51fd\u6578\u6bd4\u5982 c++<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Cat{\n    Cat();\n    Cat(string name);\n    Cat(string name,int color);\n};<\/code><\/pre>\n\n\n\n<p>\u91cd\u8f09\u770b\u4e0a\u53bb\u5f88\u7f8e\u4f46\u537b\u5f15\u767c\u4e86\u65b0\u7684\u554f\u984c\uff0c\u95dc\u9375\u5728\u65bc\u4f4d\u7f6e\u53c3\u6578\u4e0a\uff0c\u6bd4\u5982\u6709\u500b age \u53c3\u6578\u4f60\u7121\u6cd5\u5b9a\u7fa9 Cat(string name,int age) \u56e0\u7232\u9019\u500b\u7c3d\u540d\u548c \u5e36color \u7684\u53c3\u6578\u4e00\u6a23 \u6240\u4ee5\u4f60\u53ea\u80fd\u50cf\u4e0b\u9762\u9019\u6a23\u91cd\u8f09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Cat{\n    Cat();\n    Cat(string name);\n    Cat(string name,int color);\n    Cat(string name,int color,int age);\n};<\/code><\/pre>\n\n\n\n<p>\u9019\u6642\u5982\u679c\u65b0\u7684\u8abf\u7528\u8005\u53ea\u60f3\u50b3\u5165 age \u800c\u4f7f\u7528\u9ed8\u8a8d\u7684 color \u662f\u8fa6\u4e0d\u5230\u7684\uff0c\u4f60\u901a\u5e38\u53ea\u80fd\u518d\u63d0\u4f9b\u4e86\u4e00\u500b\u9ed8\u8a8d color \u5c0e\u51fa\u503c\u7d66\u8abf\u7528\u8005\u50b3\u5165\u69cb\u9020\u51fd\u6578\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u547d\u540d\u53c3\u6578<\/h2>\n\n\n\n<p>\u65b0\u8208\u7684\u8a9e\u8a00\u63d0\u4f9b\u4e86\u547d\u540d\u53c3\u6578\u4f86\u89e3\u6c7a\u9019\u4e00\u554f\u984c\uff0c\u76ee\u524d\u770b\u4f86\u61c9\u8a72\u662f\u6700\u512a\u96c5\u7684\u65b9\u6848\u4e86\uff0c\u4f46 golang \u8a9e\u8a00\u4e26\u4e0d\u652f\u6301\uff0c\u4e0d\u904e\u6211\u5011\u9084\u662f\u4f86\u770b\u4e0b\u5979\u5011\u9577\u4ec0\u9ebc\u6a23\u5b50\uff0c\u4e0b\u9762\u662f dart \u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Cat {\n  final String name;\n  final int color;\n  final int age;\n  Cat(\n    String name, { \/\/ \u82b1\u62ec\u865f\u5b9a\u7fa9\u547d\u540d\u53c3\u6578\n    int color = 1,\n    int age = 2,\n  })  : name = name,\n        color = color,\n        age = age;\n  speak() {\n    print(\n        \"i'm a cat, my name is ${name}, my favorite color is $color, my age is $age.\");\n  }\n}\n\nmain() {\n  Cat(\n    \"kate\",\n    age: 10, \/\/ \u8a2d\u7f6e\u9700\u8981\u8abf\u6574\u7684 \u547d\u540d\u53c3\u6578\n  ).speak();\n}<\/code><\/pre>\n\n\n\n<p>\u547d\u540d\u53c3\u6578\u662f\u672c\u55b5\u77e5\u66c9\u7684\u76ee\u524d\u7232\u6b62\u6700\u512a\u96c5\u7684\u65b9\u6848\uff0c\u4e26\u4e14\u547d\u540d\u53c3\u6578\u4e0d\u6b62\u53ef\u7528\u5728\u512a\u96c5\u7684\u5275\u5efa\u5c0d\u8c61\u4e0a\uff0c\u4e00\u822c\u51fd\u6578\u4e5f\u53ef\u4f7f\u7528\uff0c\u552f\u4e00\u53ef\u60dc\u7684\u662f golang \u8a9e\u8a00\u4e26\u4e0d\u652f\u6301\uff0c\u7136\u4e5f\u7121\u9700\u592a\u904e\u60cb\u60dc\u7562\u7adf golang \u7684\u512a\u52e2\u662f\u7c21\u55ae\uff0c\u4e26\u4e14\u5c31\u512a\u96c5\u7684\u5275\u5efa\u5c0d\u8c61\u4f86\u8aaa golang \u4e5f\u6709\u5408\u9069\u7684\u65b9\u6848\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Option \u63a5\u53e3<\/h2>\n\n\n\n<p>\u9019\u7a2e\u65b9\u6848\u6700\u65e9\u662f\u672c\u55b5\u5728\u770b grpc \u7684\u4ee3\u78bc\u6642\u5b78\u5230\u7684\uff0c\u9451\u65bc grpc \u7531 google \u7dad\u8b77\uff0c\u6240\u4ee5\u9019\u7a2e\u65b9\u6848\u4e5f\u7b97\u662f\u5b98\u65b9\u8a8d\u53ef\u7684\u65b9\u6848\u4e86\uff0c\u539f\u7406\u662f\u5c07\u4e0a\u8ff0 c \u65b9\u6848\u548c golang \u7684\u63a5\u53e3\u914d\u5408\u8d77\u4f86\u4f7f\u7528\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5b9a\u7fa9\u4e00\u500b\u79c1\u6709\u7684 Options \u985e\u578b<\/li><li>\u5b9a\u7fa9\u4e00\u500b\u516c\u958b\u7684 Option \u63a5\u53e3 \u63a5\u53e3\u51fd\u6578\u63a5\u53d7 Options \u6307\u91dd<\/li><li>\u63d0\u4f9b\u5404\u7a2e WithXXX \u51fd\u6578\u5be6\u73fe Option \u63a5\u53e3\u4f86\u8abf\u6574 Options \u53c3\u6578<\/li><li>\u4f7f\u7528 New(&#8230;opts Option) \u4f86\u5275\u5efa\u5c0d\u8c61<\/li><\/ol>\n\n\n\n<p>\u5982\u6b64\u7576\u9700\u8981\u64f4\u5c55\u69cb\u9020\u5c6c\u6027\u6642 \u53ea\u9700\u8981\u4fee\u6539 Options \u985e\u578b \u4e26\u589e\u52a0\u4e00\u500b WithXXX \u51fd\u6578\u5373\u53ef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ \u5b9a\u7fa9\u9ed8\u8a8d\u53c3\u6578\u503c\nvar defaultOptions = catOptions{\n\tname:  `none`,\n\tcolor: 1,\n\tage:   5,\n}\n\n\/\/ \u5b9a\u7fa9 \u79c1\u6709\u7684 \u53c3\u6578 \u985e\u578b\ntype catOptions struct {\n\tname  string\n\tcolor int\n\tage   int\n}\n\n\/\/ CatOption \u521d\u59cb\u5316\u63a5\u53e3\ntype CatOption interface {\n\tapply(*catOptions)\n}\n\n\/\/ \u5be6\u73fe CatOption \u63a5\u53e3 \u5e6b\u52a9\u5275\u5efa With \u51fd\u6578\ntype funcCatOption struct {\n\tf func(*catOptions)\n}\n\nfunc (fdo *funcCatOption) apply(do *catOptions) {\n\tfdo.f(do)\n}\nfunc newCatFuncOption(f func(*catOptions)) *funcCatOption {\n\treturn &amp;funcCatOption{\n\t\tf: f,\n\t}\n}\n\nfunc WithCatName(name string) CatOption {\n\treturn newCatFuncOption(func(o *catOptions) {\n\t\to.name = name\n\t})\n}\nfunc WithCatColor(color int) CatOption {\n\treturn newCatFuncOption(func(o *catOptions) {\n\t\to.color = color\n\t})\n}\nfunc WithCatAge(age int) CatOption {\n\treturn newCatFuncOption(func(o *catOptions) {\n\t\to.age = age\n\t})\n}\n\ntype Cat struct {\n\topts *catOptions \/\/ \u4fdd\u5b58 \u69cb\u9020\u53c3\u6578\n}\n\nfunc NewCat(opt ...CatOption) *Cat {\n\topts := defaultOptions\n\tfor _, o := range opt {\n\t\to.apply(&amp;opts)\n\t}\n\treturn &amp;Cat{\n\t\topts: &amp;opts,\n\t}\n}\nfunc (c *Cat) Speak() {\n\tfmt.Printf(\"i'm a cat, my name is %s, my favorite color is %d, my age is %d.\\n\",\n\t\tc.opts.name, c.opts.color, c.opts.age,\n\t)\n}\nfunc main() {\n\tcat := NewCat(\n\t\tWithCatName(`kate`),\n\t\tWithCatAge(2),\n\t)\n\tcat.Speak()\n}<\/code><\/pre>\n\n\n\n<p>\u96d6\u7136\u548c\u547d\u540d\u53c3\u6578\u6bd4\u8d77\u4f86\u9ebb\u7169\u4e86\u4e00\u9ede\uff0c\u4f46\u5728\u6c92\u6709\u547d\u540d\u53c3\u6578\u6642\u5c0d\u65bc\u9700\u8981\u64f4\u5c55\u6216\u521d\u59cb\u5316\u53c3\u6578\u8907\u96dc\u6642\u61c9\u8a72\u7b97\u662f\u6700\u512a\u96c5\u7684\u65b9\u6848\u4e4b\u4e00\u4e86\uff0c\u6b64\u5916\u9019\u7a2e\u5beb\u6cd5\u4e0d\u9650\u8207 golang \u4efb\u4f55\u652f\u6301\u63a5\u53e3\u7684\u8a9e\u8a00\u90fd\u53ef\u4ee5\u4f7f\u7528\u9019\u7a2e\u65b9\u5f0f\u4f86\u8b93\u5275\u5efa\u5c0d\u8c61\u7684\u5be6\u73fe\u548c\u8abf\u7528\u8b8a\u5f97\u512a\u96c5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7d39 \u5982\u4f55 \u512a\u96c5\u7684\u5be6\u73fe New \u51fd\u6578 \u4e26\u4f7f\u5176\u4fdd\u6301\u6613\u65bc\u8abf\u7528\u548c\u64f4\u5c55<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11,22,2],"tags":[12],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-golang","category-elegant-code","category-code","tag-golang"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.king011.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":19,"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions\/62"}],"wp:attachment":[{"href":"https:\/\/blog.king011.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.king011.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.king011.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}