{"id":5466,"date":"2023-01-05T12:43:16","date_gmt":"2023-01-05T04:43:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5466"},"modified":"2023-01-05T12:43:16","modified_gmt":"2023-01-05T04:43:16","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84classes%e7%89%b9%e6%80%a7%e5%8f%8a%e5%85%b6%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84classes%e7%89%b9%e6%80%a7%e5%8f%8a%e5%85%b6%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684Classes\u7279\u6027\u53ca\u5176\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, classes\"><\/p>\n<h1>\u4f7f\u7528 TypeScript \u7684 Classes<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u652f\u63f4\u985e\u5225\uff08classes\uff09\u3002\u985e\u5225\u662f\u4e00\u7a2e\u5c01\u88dd\u7269\u4ef6\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c07\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u5c01\u88dd\u5728\u4e00\u8d77\uff0c\u4f7f\u5f97\u7a0b\u5f0f\u78bc\u66f4\u52a0\u6e05\u6670\u6613\u8b80\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <span style=\"font-weight: bold;\">class<\/span> \u95dc\u9375\u5b57\u4f86\u5b9a\u7fa9\u4e00\u500b\u985e\u5225\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528 <span style=\"font-weight: bold;\">constructor<\/span> \u4f86\u5b9a\u7fa9\u985e\u5225\u7684\u5efa\u69cb\u5b50\uff0c\u4ee5\u53ca\u4f7f\u7528 <span style=\"font-weight: bold;\">public<\/span>\u3001<span style=\"font-weight: bold;\">private<\/span> \u548c <span style=\"font-weight: bold;\">protected<\/span> \u4f86\u5b9a\u7fa9\u985e\u5225\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u8981\u5b9a\u7fa9\u4e00\u500b\u540d\u70ba <span style=\"font-weight: bold;\">Person<\/span> \u7684\u985e\u5225\uff0c\u4e26\u4e14\u5b83\u6709\u4e00\u500b\u540d\u70ba <span style=\"font-weight: bold;\">name<\/span> \u7684\u5c6c\u6027\uff0c\u4ee5\u53ca\u4e00\u500b\u540d\u70ba <span style=\"font-weight: bold;\">sayHello()<\/span> \u7684\u65b9\u6cd5\uff0c\u90a3\u9ebc\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<pre class=\"brush: typescript\">\nclass Person {\n    private name: string;\n\n    constructor(name: string) {\n        this.name = name;\n    }\n\n    public sayHello(): void {\n        console.log(`Hello, my name is ${this.name}`);\n    }\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <span style=\"font-weight: bold;\">class<\/span> \u95dc\u9375\u5b57\u4f86\u5b9a\u7fa9\u4e00\u500b\u540d\u70ba <span style=\"font-weight: bold;\">Person<\/span> \u7684\u985e\u5225\uff0c\u4e26\u4e14\u4f7f\u7528 <span style=\"font-weight: bold;\">constructor<\/span> \u4f86\u5b9a\u7fa9\u985e\u5225\u7684\u5efa\u69cb\u5b50\uff0c\u4e26\u4e14\u4f7f\u7528 <span style=\"font-weight: bold;\">private<\/span> \u4f86\u5b9a\u7fa9\u985e\u5225\u7684\u5c6c\u6027\uff0c\u4ee5\u53ca\u4f7f\u7528 <span style=\"font-weight: bold;\">public<\/span> \u4f86\u5b9a\u7fa9\u985e\u5225\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <span style=\"font-weight: bold;\">new<\/span> \u95dc\u9375\u5b57\u4f86\u5efa\u7acb\u4e00\u500b Person \u7269\u4ef6\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528 <span style=\"font-weight: bold;\">sayHello()<\/span> \u65b9\u6cd5\u4f86\u8aaa\u51fa\u8a72\u7269\u4ef6\u7684\u540d\u5b57\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet person = new Person(\"John\");\nperson.sayHello(); \/\/ Hello, my name is John\n<\/pre>\n<p>\u5f9e\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u770b\u51fa\uff0c\u4f7f\u7528 TypeScript \u7684 classes \u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u5bb9\u6613\u5730\u5c01\u88dd\u7269\u4ef6\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u52a0\u6e05\u6670\u5730\u5b9a\u7fa9\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u4f7f\u5f97\u7a0b\u5f0f\u78bc\u66f4\u52a0\u6613\u8b80\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3TypeScript\u4e2d\u7684Classes\u7279\u6027\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Classes\u4f86\u5efa\u7acb\u7269\u4ef6\u5c0e\u5411\u7684\u7a0b\u5f0f\u78bc\uff0c\u4e26\u77ad\u89e3Classes\u7684\u529f\u80fd\u53ca\u5176\u512a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5466","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1qa","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5466"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5466\/revisions"}],"predecessor-version":[{"id":5467,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5466\/revisions\/5467"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}