{"id":53503,"date":"2024-12-03T16:01:30","date_gmt":"2024-12-03T08:01:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53503\/"},"modified":"2024-12-03T16:01:30","modified_gmt":"2024-12-03T08:01:30","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%bc%96%e7%a8%8b%ef%bc%88oop%ef%bc%89%ef%bc%9a%e9%80%9a%e8%bf%87%e6%b8%85%e6%99%b0%e7%9a%84%e7%a4%ba%e4%be%8b%e7%90%86%e8%a7%a3%e6%94%af%e6%9f%b1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53503\/","title":{"rendered":"\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\uff1a\u901a\u8fc7\u6e05\u6670\u7684\u793a\u4f8b\u7406\u89e3\u652f\u67f1"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\uff1a\u901a\u8fc7\u6e05\u6670\u7684\u793a\u4f8b\u7406\u89e3\u652f\u67f1<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\uff1a\u901a\u8fc7\u6e05\u6670\u7684\u793a\u4f8b\u7406\u89e3\u652f\u67f1\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733023486674bd6fe5f7ad.jpg\" class=\"aligncenter\" title=\"\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\uff1a\u901a\u8fc7\u6e05\u6670\u7684\u793a\u4f8b\u7406\u89e3\u652f\u67f1\u63d2\u56fe\" alt=\"\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\uff1a\u901a\u8fc7\u6e05\u6670\u7684\u793a\u4f8b\u7406\u89e3\u652f\u67f1\u63d2\u56fe\" \/><\/p>\n<p>\u563f\u5f00\u53d1\u8005\uff01\u4eca\u5929\u6211\u4eec\u8981\u8ba8\u8bba\u7684\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08oop\uff09\u3002\u8fd9\u79cd\u8303\u4f8b\u5bf9\u4e8e\u4f7f\u7528\u201c\u5bf9\u8c61\u201d\u7ec4\u7ec7\u6570\u636e\u548c\u884c\u4e3a\u81f3\u5173\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u51c6\u5907\u6c42\u804c\u9762\u8bd5\uff0c\u638c\u63e1\u8fd9\u4e9b\u6982\u5ff5\u4f1a\u8ba9\u4e00\u5207\u53d8\u5f97\u4e0d\u540c\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u4ee5\u6e05\u6670\u5b9e\u7528\u7684\u65b9\u5f0f\u63a2\u7d22 oop \u7684\u56db\u5927\u652f\u67f1\uff0c\u5e76\u4e3e\u4f8b\u5e2e\u52a9\u60a8\u8f7b\u677e\u7406\u89e3\u4e00\u5207\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff1f <\/h2>\n<p>oop \u57fa\u4e8e\u56db\u4e2a\u4e3b\u8981\u652f\u67f1\uff1a<\/p>\n<ol>\n<li><strong>\u5c01\u88c5<\/strong><\/li>\n<li><strong>\u7ee7\u627f<\/strong><\/li>\n<li><strong>\u591a\u6001\u6027<\/strong><\/li>\n<li><strong>\u62bd\u8c61<\/strong><\/li>\n<\/ol>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7 javascript \u793a\u4f8b\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u6bcf\u4e2a\u652f\u67f1\u3002<\/p>\n<h2> 1. \u5c01\u88c5 <\/h2>\n<p>\u5c01\u88c5\u5c31\u50cf\u5c06\u60a8\u7684\u7269\u54c1\u5b58\u653e\u5728\u4e00\u4e2a\u76d2\u5b50\u91cc\u3002\u60a8\u5c06\u6240\u9700\u7684\u4e00\u5207\u653e\u5165\u5176\u4e2d\u5e76\u63a7\u5236\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002\u8fd9\u6709\u52a9\u4e8e\u4fdd\u62a4\u5b58\u50a8\u7684\u6570\u636e\u5e76\u786e\u4fdd\u5bf9\u8c61\u7684\u5185\u90e8\u72b6\u6001\u4fdd\u6301\u5b89\u5168\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>class user {\n    constructor(name, age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    \/\/ public method\n    displayinfo() {\n        return `${this.name}, ${this.age} years old`;\n    }\n\n    \/\/ private method\n    _checkage() {\n        return this.age &gt;= 18 ? 'an adult' : 'a minor';\n    }\n\n    displaystatus() {\n        return `${this.name} is ${this._checkage()}.`;\n    }\n}\n\nconst user = new user('alice', 22);\nconsole.log(user.displayinfo()); \/\/ alice, 22 years old\nconsole.log(user.displaystatus()); \/\/ alice is an adult\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c_checkage \u662f\u4e0d\u5e94\u76f4\u63a5\u8bbf\u95ee\u7684\u65b9\u6cd5\u3002\u5b83\u5728\u5185\u90e8\u7528\u4e8e\u5e2e\u52a9\u786e\u5b9a\u7528\u6237\u7684\u72b6\u6001\uff0c\u540c\u65f6\u4fdd\u6301\u903b\u8f91\u6709\u5e8f\u3002<\/p>\n<h2> 2. \u7ee7\u627f <\/h2>\n<p>\u7ee7\u627f\u5141\u8bb8\u4e00\u4e2a\u7c7b\uff08\u5b50\u7c7b\uff09\u4ece\u53e6\u4e00\u4e2a\u7c7b\uff08\u8d85\u7c7b\uff09\u7ee7\u627f\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8fd9\u4f7f\u5f97\u91cd\u7528\u4ee3\u7801\u548c\u521b\u5efa\u7c7b\u5c42\u6b21\u7ed3\u6784\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>class animal {\n    constructor(name) {\n        this.name = name;\n    }\n\n    makesound() {\n        return `${this.name} makes a sound.`;\n    }\n}\n\nclass dog extends animal {\n    makesound() {\n        return `${this.name} barks.`;\n    }\n}\n\nclass cat extends animal {\n    makesound() {\n        return `${this.name} meows.`;\n    }\n}\n\nconst mydog = new dog('rex');\nconst mycat = new cat('mia');\n\nconsole.log(mydog.makesound()); \/\/ rex barks.\nconsole.log(mycat.makesound()); \/\/ mia meows.\n<\/pre>\n<p>\u8fd9\u91cc\uff0cdog \u548c cat \u90fd\u7ee7\u627f\u81ea animal\u3002\u6bcf\u4e2a\u90fd\u5b9e\u73b0\u4e86\u81ea\u5df1\u7684\u58f0\u97f3\uff0c\u5c55\u793a\u4e86\u7ee7\u627f\u5982\u4f55\u5141\u8bb8\u81ea\u5b9a\u4e49\u884c\u4e3a\u800c\u4e0d\u91cd\u590d\u4ee3\u7801\u3002<\/p>\n<h2> 3\u3001\u591a\u6001\u6027 <\/h2>\n<p>\u591a\u6001\u662f\u4e0d\u540c\u5bf9\u8c61\u4ee5\u4e0d\u540c\u65b9\u5f0f\u54cd\u5e94\u540c\u4e00\u65b9\u6cd5\u7684\u80fd\u529b\u3002\u8fd9\u5141\u8bb8\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u65b9\u6cd5\u6839\u636e\u5bf9\u8c61\u7684\u7c7b\u578b\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>class shape {\n    area() {\n        return 0;\n    }\n}\n\nclass rectangle extends shape {\n    constructor(width, height) {\n        super();\n        this.width = width;\n        this.height = height;\n    }\n\n    area() {\n        return this.width * this.height;\n    }\n}\n\nclass circle extends shape {\n    constructor(radius) {\n        super();\n        this.radius = radius;\n    }\n\n    area() {\n        return math.pi * math.pow(this.radius, 2);\n    }\n}\n\nconst shapes = [new rectangle(10, 5), new circle(3)];\n\nshapes.foreach(shape =&gt; {\n    console.log(`area: ${shape.area()}`);\n});\n\n\/\/ output:\n\/\/ area: 50\n\/\/ area: 28.274333882308138\n<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u77e9\u5f62\u548c\u5706\u5f62\u90fd\u6709\u81ea\u5df1\u7684\u9762\u79ef\u65b9\u6cd5\uff0c\u4f46\u6839\u636e\u5f62\u72b6\u7c7b\u578b\u8c03\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002\u8fd9\u5c31\u662f\u591a\u6001\u6027\u7684\u4f5c\u7528\uff01<\/p>\n<h2> 4. \u62bd\u8c61 <\/h2>\n<p>\u62bd\u8c61\u662f\u9690\u85cf\u590d\u6742\u7ec6\u8282\u5e76\u4ec5\u66b4\u9732\u5fc5\u8981\u5185\u5bb9\u7684\u8fc7\u7a0b\u3002\u5728 oop \u4e2d\uff0c\u8fd9\u5141\u8bb8\u60a8\u4f7f\u7528\u5bf9\u8c61\u800c\u65e0\u9700\u4e86\u89e3\u5b83\u4eec\u5982\u4f55\u5de5\u4f5c\u7684\u6240\u6709\u590d\u6742\u6027\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>class Car {\n    constructor(brand, model) {\n        this.brand = brand;\n        this.model = model;\n    }\n\n    start() {\n        console.log('Car started.');\n    }\n\n    stop() {\n        console.log('Car stopped.');\n    }\n}\n\nclass ElectricCar extends Car {\n    charge() {\n        console.log('Electric car charging.');\n    }\n}\n\nconst myElectricCar = new ElectricCar('Tesla', 'Model 3');\nmyElectricCar.start(); \/\/ Car started.\nmyElectricCar.charge(); \/\/ Electric car charging.\n<\/pre>\n<p>\u8fd9\u91cc\uff0ccar \u7c7b\u63d0\u4f9b\u4e86\u57fa\u672c\u65b9\u6cd5\uff0c\u800c electriccar \u5219\u6dfb\u52a0\u4e86\u5145\u7535\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u5728\u4e0d\u77e5\u9053\u6bcf\u4e2a\u90e8\u4ef6\u5982\u4f55\u5de5\u4f5c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u6c7d\u8f66 &#8211; \u60a8\u53ea\u9700\u8981\u77e5\u9053\u5982\u4f55\u542f\u52a8\u548c\u5145\u7535\u3002<\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u5c31\u662f\u8fd9\u6837\uff01\u73b0\u5728\uff0c\u60a8\u5bf9\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u56db\u5927\u652f\u67f1\u6709\u4e86\u66f4\u6e05\u6670\u7684\u4e86\u89e3\uff1a\u5c01\u88c5\u3001\u7ee7\u627f\u3001\u591a\u6001\u6027\u548c\u62bd\u8c61\u3002\u8fd9\u4e9b\u6982\u5ff5\u5bf9\u4e8e\u7f16\u5199\u66f4\u6709\u7ec4\u7ec7\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u5728\u60a8\u7684\u9879\u76ee\u4e2d\u4e0d\u65ad\u7ec3\u4e60\u548c\u5e94\u7528\u8fd9\u4e9b\u539f\u5219\uff0c\u60a8\u5c06\u505a\u597d\u5145\u5206\u51c6\u5907\u6765\u5e94\u5bf9\u9762\u8bd5\u548c\u4f5c\u4e3a\u5f00\u53d1\u4eba\u5458\u7684\u65e5\u5e38\u6311\u6218\uff01<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\uff1a\u901a\u8fc7\u6e05\u6670\u7684\u793a\u4f8b\u7406\u89e3\u652f\u67f1\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   VUE3 + element-plus \u4e2d\uff0cthis.$emit \u5931\u6548\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\uff1a\u901a\u8fc7\u6e05\u6670\u7684\u793a\u4f8b\u7406\u89e3\u652f\u67f1 \u6536\u85cf \u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\uff1a\u901a\u8fc7\u6e05\u6670\u7684\u793a\u4f8b\u7406\u89e3\u652f\u67f1\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002 \u563f\u5f00\u53d1\u8005\uff01\u4eca\u5929\u6211\u4eec\u8981\u8ba8\u8bba\u7684\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08oop\uff09\u3002\u8fd9\u79cd\u8303\u4f8b\u5bf9\u4e8e\u4f7f\u7528\u201c\u5bf9\u8c61\u201d\u7ec4\u7ec7\u6570\u636e\u548c\u884c\u4e3a\u81f3\u5173\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u51c6\u5907\u6c42\u804c\u9762\u8bd5\uff0c\u638c\u63e1\u8fd9\u4e9b\u6982\u5ff5\u4f1a\u8ba9\u4e00\u5207\u53d8\u5f97\u4e0d\u540c\u3002 \u6211\u4eec\u5c06\u4ee5\u6e05\u6670\u5b9e\u7528\u7684\u65b9\u5f0f\u63a2\u7d22 oop \u7684\u56db\u5927\u652f\u67f1\uff0c\u5e76\u4e3e\u4f8b\u5e2e\u52a9\u60a8\u8f7b\u677e\u7406\u89e3\u4e00\u5207\u3002 \u4ec0\u4e48\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff1f oop \u57fa\u4e8e\u56db\u4e2a\u4e3b\u8981\u652f\u67f1\uff1a \u5c01\u88c5 \u7ee7\u627f \u591a\u6001\u6027 \u62bd\u8c61 \u8ba9\u6211\u4eec\u901a\u8fc7 javascript \u793a\u4f8b\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u6bcf\u4e2a\u652f\u67f1\u3002 1. \u5c01\u88c5 \u5c01\u88c5\u5c31\u50cf\u5c06\u60a8\u7684\u7269\u54c1\u5b58\u653e\u5728\u4e00\u4e2a\u76d2\u5b50\u91cc\u3002\u60a8\u5c06\u6240\u9700\u7684\u4e00\u5207\u653e\u5165\u5176\u4e2d\u5e76\u63a7\u5236\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002\u8fd9\u6709\u52a9\u4e8e\u4fdd\u62a4\u5b58\u50a8\u7684\u6570\u636e\u5e76\u786e\u4fdd\u5bf9\u8c61\u7684\u5185\u90e8\u72b6\u6001\u4fdd\u6301\u5b89\u5168\u3002 \u4f8b\u5b50\uff1a class user { constructor(name, age) { this.name = name; this.age = age; } \/\/ public method displayinfo() { return `${this.name}, ${this.age} years old`; } \/\/ private method _checkage() { return this.age &gt;= 18 [&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-53503","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53503","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=53503"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53503\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}