{"id":53566,"date":"2024-12-03T16:24:45","date_gmt":"2024-12-03T08:24:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53566\/"},"modified":"2024-12-03T16:24:45","modified_gmt":"2024-12-03T08:24:45","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%bc%96%e7%a8%8b-%e5%b0%81%e8%a3%85","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53566\/","title":{"rendered":"\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u2014\u2014\u5c01\u88c5"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u2014\u2014\u5c01\u88c5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u2014\u2014\u5c01\u88c5\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733105930674d190aa248d.jpg\" class=\"aligncenter\" title=\"\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u2014\u2014\u5c01\u88c5\u63d2\u56fe\" alt=\"\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u2014\u2014\u5c01\u88c5\u63d2\u56fe\" \/><\/p>\n<h2> <em>\u4ec0\u4e48\uff1f\uff1f\uff1f<\/em> <\/h2>\n<blockquote>\n<p><strong><em>\u5c01\u88c5<\/em><\/strong>\u662f\u5c06\u6570\u636e\u548c\u51fd\u6570\u6346\u7ed1\u5230\u4e00\u4e2a\u5355\u5143\uff08\u5373\u80f6\u56ca\uff09\u4e2d\u7684\u8fc7\u7a0b\uff0c\u5b83\u8fd8\u53ef\u4ee5\u9650\u5236\u5bf9\u67d0\u4e9b\u6570\u636e\/\u65b9\u6cd5\u7684\u8bbf\u95ee\u3002<\/p>\n<\/blockquote>\n<p>\u5b83\u662f oop \u7684\u56db\u5927\u652f\u67f1\u4e4b\u4e00\uff0c\u5176\u4ed6\u4e09\u8005\u5206\u522b\u662f\u7ee7\u627f\u3001\u591a\u6001\u6027\u548c\u6570\u636e\u62bd\u8c61\u3002<\/p>\n<h2> <em>\u4e3a\u4ec0\u4e48\uff1f<\/em> <\/h2>\n<p>\u91c7\u53d6\u76f2\u76ee\u5047\u8bbe\u5e76\u5728\u6240\u6709\u5730\u65b9\u7ee7\u7eed\u4f7f\u7528\u5c01\u88c5\u4f1a\u66f4\u5bb9\u6613\uff0c\u4f46\u4e86\u89e3\u539f\u56e0\u5f88\u91cd\u8981\uff0c\u8fd9\u6837\u60a8\u624d\u80fd\u4ee5\u6b63\u786e\u7684\u65b9\u5f0f\u4f7f\u7528\u5b83\u3002 <\/p>\n<p>\u8ba9\u6211\u4eec\u5c1d\u8bd5\u901a\u8fc7\u67e5\u770b\u793a\u4f8b\u4efb\u52a1\u6765\u7406\u89e3\u539f\u56e0\u3002<\/p>\n<h4> \u4efb\u52a1\uff1a <\/h4>\n<p>\u6784\u5efa\u4e00\u4e2a\u5b66\u751f\u6210\u7ee9\u8ba1\u7b97\u5668\uff0c<\/p>\n<ul>\n<li>\u8ba1\u7b97\u5e73\u5747\u5206<\/li>\n<li>\u786e\u5b9a\u5b66\u751f\u662f\u5426\u5931\u8d25\u6216\u901a\u8fc7<\/li>\n<li>\u5982\u679c\u4efb\u4f55\u4e3b\u9898\u6807\u8bb0\u65e0\u6548\uff0c\u5219\u629b\u51fa\u9519\u8bef ( &lt; 0 || &gt; 100)<\/li>\n<\/ul>\n<p><strong><em>\u65b9\u6848\u4e00\uff1a\u975e\u5c01\u88c5\u65b9\u5f0f<\/em><\/strong><\/p>\n<p>\u8fd9\u4e2a\u60f3\u6cd5\u53ea\u662f\u4e3a\u4e86\u89e3\u51b3\u95ee\u9898\uff0c\u6240\u4ee5\u6211\u9009\u62e9\u4e86<em>\u8fc7\u7a0b\u5f0f\u7f16\u7a0b<\/em>\u5b9e\u73b0\u5b83\u7684\u65b9\u5f0f\uff0c\u6211\u76f8\u4fe1\u5b83\u53ef\u4ee5\u663e\u793a\u51fa\u5f88\u597d\u7684\u5bf9\u6bd4\u5e76\u4f7f\u95ee\u9898\u770b\u8d77\u6765\u66f4\u660e\u663e\u3002<\/p>\n<pre>type subject = \"english\" | \"maths\";\n\ninterface istudent {\n  name: string;\n  marks: record&lt;subject, number&gt;;\n}\n\n\/\/ receive input\nconst studentinput: istudent = {\n  name: \"john\",\n  marks: {\n    english: 100,\n    maths: 100,\n  },\n};\n\n\/\/ step1: validate the provided marks\nobject.keys(studentinput.marks).foreach((subjectname) =&gt; {\n  const mark = studentinput.marks[subjectname as subject];\n  if (mark &gt; 100 || mark &lt; 0) {\n    throw new error(`invlid mark found`);\n  }\n});\n\n\/\/ step2: find the total marks\nconst totalmarks = object.keys(studentinput.marks).reduce(\n  (accumulator: number, current: string) =&gt;\n    studentinput.marks[current as subject] + accumulator,\n  0\n);\n\n\/\/ step3: find the average\nconst average = totalmarks \/ object.keys(studentinput.marks).length;\n\n\/\/ step4: find the result\nconst boolresult = average &gt; 40;\n\n\/\/ step 5: print result\nconsole.log(boolresult);\nconsole.log(average);\n\n<\/pre>\n<p><strong><em>\u89e3\u51b3\u65b9\u6848 1 \u7684\u95ee\u9898\uff1a<\/em><\/strong><\/p>\n<p>\u8fd9\u786e\u5b9e\u8fbe\u5230\u4e86\u9884\u671f\u7684\u7ed3\u679c\uff0c\u4f46\u4e5f\u5b58\u5728\u4e00\u4e9b\u4e0e\u4e4b\u76f8\u5173\u7684\u95ee\u9898\u3002\u4ec5\u4e3e\u51e0\u4f8b\uff0c<\/p>\n<blockquote>\n<ol>\n<li>\u8fd9\u91cc\u7684\u6bcf\u4e2a\u5b9e\u73b0\u90fd\u662f\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\uff0c\u5e76\u4e14\u672a\u6765\u7684\u8d21\u732e\u8005\u65e0\u6cd5\u63a7\u5236\u5176\u4f7f\u7528\u3002<\/li>\n<li>\u6570\u636e\u548c\u64cd\u4f5c\u662f\u5206\u5f00\u7684\uff0c\u56e0\u6b64\u5f88\u96be\u8ffd\u8e2a\u54ea\u4e9b\u51fd\u6570\u5f71\u54cd\u6570\u636e\u3002\u60a8\u5fc5\u987b\u4ed4\u7ec6\u68c0\u67e5\u6bcf\u4e00\u6bb5\u4ee3\u7801\u624d\u80fd\u4e86\u89e3\u8c03\u7528\u7684\u5185\u5bb9\u4ee5\u53ca\u6267\u884c\u7684\u4e00\u90e8\u5206\u3002<\/li>\n<li>\u968f\u7740\u903b\u8f91\u7684\u6269\u5c55\uff0c\u51fd\u6570\u53d8\u5f97\u66f4\u96be\u7ba1\u7406\u3002\u7531\u4e8e\u7d27\u5bc6\u8026\u5408\uff0c\u66f4\u6539\u53ef\u80fd\u4f1a\u7834\u574f\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h2> \u5982\u4f55\u89e3\u51b3\u95ee\u9898\uff1f <\/h2>\n<p>\u901a\u8fc7\u5408\u5e76\u5c01\u88c5\u6216\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u4e24\u4e2a\u6b65\u9aa4\u4f7f\u5176\u66f4\u52a0\u660e\u663e\uff0c<\/p>\n<ol>\n<li>\u5bf9\u6570\u636e\u548c\u529f\u80fd\u7684\u53d7\u63a7\u8bbf\u95ee<\/li>\n<li>\u5c06\u6570\u636e\u4e0e\u884c\u4e3a\u6346\u7ed1<\/li>\n<\/ol>\n<p><strong><em>\u89e3\u51b3\u65b9\u68482\uff1a\u5c01\u88c5\u65b9\u5f0f<\/em><\/strong><\/p>\n<pre>type SubjectNames = \"english\" | \"maths\";\n\ninterface IStudent {\n  name: string;\n  marks: Record&lt;SubjectNames, number&gt;;\n}\n\nclass ResultCalculator {\n  protected student: IStudent;\n  constructor(student: IStudent) {\n    this.student = student;\n  }\n\n  isPassed(): boolean {\n    let resultStatus = true;\n    Object.keys(this.student.marks).forEach((subject: string) =&gt; {\n      if (this.student.marks[subject as SubjectNames] &lt; 40) {\n        resultStatus = false;\n      }\n    });\n    return resultStatus;\n  }\n\n  getAverage(): number {\n    this.validateMarks();\n    return this.totalMarks() \/ this.subjectCount();\n  }\n\n  private validateMarks() {\n    Object.keys(this.student.marks).forEach((subject: string) =&gt; {\n      if (\n        this.student.marks[subject as SubjectNames] &lt; 0 ||\n        this.student.marks[subject as SubjectNames] &gt; 100\n      ) {\n        throw new Error(`invalid mark`);\n      }\n    });\n  }\n\n  private totalMarks() {\n    return Object.keys(this.student.marks).reduce(\n      (acc, curr) =&gt; this.student.marks[curr as SubjectNames] + acc,\n      0\n    );\n  }\n\n  private subjectCount() {\n    return Object.keys(this.student.marks).length;\n  }\n}\n\n\/\/ Receive Input\nconst a: IStudent = {\n  name: \"jingleheimer schmidt\",\n  marks: {\n    english: 100,\n    maths: 100,\n  },\n};\n\n\/\/ Create an encapsulated object\nconst result = new ResultCalculator(a);\n\n\/\/ Perform operations &amp; print results\nconsole.log(result.isPassed());\nconsole.log(result.getAverage());\n\n<\/pre>\n<p>\u6ce8\u610f\u4e0a\u8ff0\u89e3\u51b3\u65b9\u6848\uff0c<\/p>\n<ol>\n<li>\u65b9\u6cd5totalmarks\u3001subjectcount\u3001validatemarks \u548c\u6210\u5458\u53d8\u91cfstudent \u4e0d\u516c\u5f00\uff0c\u53ea\u80fd\u7531\u7c7b\u5bf9\u8c61\u4f7f\u7528\u3002 <\/li>\n<\/ol>\n<p>2.\u6570\u636e\u5b66\u751f\u4e0e\u5176\u6bcf\u4e00\u4e2a\u884c\u4e3a\u90fd\u6346\u7ed1\u5728\u4e00\u8d77\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u2014\u2014\u5c01\u88c5\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\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<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u66f4\u597d\u5730\u5728\u7167\u7247\u4e2d\u8868\u73b0\u79cb\u51ac\u6c1b\u56f4\u611f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u2014\u2014\u5c01\u88c5 \u6536\u85cf \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\u300a\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u2014\u2014\u5c01\u88c5\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01 \u4ec0\u4e48\uff1f\uff1f\uff1f \u5c01\u88c5\u662f\u5c06\u6570\u636e\u548c\u51fd\u6570\u6346\u7ed1\u5230\u4e00\u4e2a\u5355\u5143\uff08\u5373\u80f6\u56ca\uff09\u4e2d\u7684\u8fc7\u7a0b\uff0c\u5b83\u8fd8\u53ef\u4ee5\u9650\u5236\u5bf9\u67d0\u4e9b\u6570\u636e\/\u65b9\u6cd5\u7684\u8bbf\u95ee\u3002 \u5b83\u662f oop \u7684\u56db\u5927\u652f\u67f1\u4e4b\u4e00\uff0c\u5176\u4ed6\u4e09\u8005\u5206\u522b\u662f\u7ee7\u627f\u3001\u591a\u6001\u6027\u548c\u6570\u636e\u62bd\u8c61\u3002 \u4e3a\u4ec0\u4e48\uff1f \u91c7\u53d6\u76f2\u76ee\u5047\u8bbe\u5e76\u5728\u6240\u6709\u5730\u65b9\u7ee7\u7eed\u4f7f\u7528\u5c01\u88c5\u4f1a\u66f4\u5bb9\u6613\uff0c\u4f46\u4e86\u89e3\u539f\u56e0\u5f88\u91cd\u8981\uff0c\u8fd9\u6837\u60a8\u624d\u80fd\u4ee5\u6b63\u786e\u7684\u65b9\u5f0f\u4f7f\u7528\u5b83\u3002 \u8ba9\u6211\u4eec\u5c1d\u8bd5\u901a\u8fc7\u67e5\u770b\u793a\u4f8b\u4efb\u52a1\u6765\u7406\u89e3\u539f\u56e0\u3002 \u4efb\u52a1\uff1a \u6784\u5efa\u4e00\u4e2a\u5b66\u751f\u6210\u7ee9\u8ba1\u7b97\u5668\uff0c \u8ba1\u7b97\u5e73\u5747\u5206 \u786e\u5b9a\u5b66\u751f\u662f\u5426\u5931\u8d25\u6216\u901a\u8fc7 \u5982\u679c\u4efb\u4f55\u4e3b\u9898\u6807\u8bb0\u65e0\u6548\uff0c\u5219\u629b\u51fa\u9519\u8bef ( &lt; 0 || &gt; 100) \u65b9\u6848\u4e00\uff1a\u975e\u5c01\u88c5\u65b9\u5f0f \u8fd9\u4e2a\u60f3\u6cd5\u53ea\u662f\u4e3a\u4e86\u89e3\u51b3\u95ee\u9898\uff0c\u6240\u4ee5\u6211\u9009\u62e9\u4e86\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\u5b9e\u73b0\u5b83\u7684\u65b9\u5f0f\uff0c\u6211\u76f8\u4fe1\u5b83\u53ef\u4ee5\u663e\u793a\u51fa\u5f88\u597d\u7684\u5bf9\u6bd4\u5e76\u4f7f\u95ee\u9898\u770b\u8d77\u6765\u66f4\u660e\u663e\u3002 type subject = &#8220;english&#8221; | &#8220;maths&#8221;; interface istudent { name: string; marks: record&lt;subject, number&gt;; } \/\/ receive input const studentinput: istudent = { name: &#8220;john&#8221;, marks: { english: 100, maths: 100, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53566","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53566"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53566\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}