{"id":4996,"date":"2023-01-05T11:35:09","date_gmt":"2023-01-05T03:35:09","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4996"},"modified":"2023-01-05T11:35:09","modified_gmt":"2023-01-05T03:35:09","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e6%8e%a5%e5%8f%a3interfaces-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e6%8e%a5%e5%8f%a3interfaces-2\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528TypeScript\u63a5\u53e3(interfaces)"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, Interfaces\"><\/p>\n<h1>\u4f7f\u7528 TypeScript \u63a5\u53e3(interfaces)\u7684\u5b9e\u73b0<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e\u7531 Microsoft \u958b\u767c\u7684 JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u985e\u578b\u6aa2\u67e5\u548c\u975c\u614b\u5206\u6790\u529f\u80fd\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u958b\u767c\u5927\u578b JavaScript \u61c9\u7528\u7a0b\u5e8f\u3002TypeScript \u63a5\u53e3(interfaces)\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u548c\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u4e2d\u7684\u7269\u4ef6\u548c\u51fd\u6578\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u4eba\u54e1\u66f4\u5bb9\u6613\u5730\u69cb\u5efa\u548c\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u6e1b\u5c11\u51fa\u932f\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<p><!--more--><\/p>\n<p>TypeScript \u63a5\u53e3(interfaces)\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u500b\u7269\u4ef6\u7684\u7d50\u69cb\uff0c\u4e26\u6307\u5b9a\u8a72\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u500b\u51fd\u6578\u7684\u53c3\u6578\u548c\u8fd4\u56de\u503c\uff0c\u4ee5\u53ca\u5b9a\u7fa9\u4e00\u500b\u985e\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u500b\u6a21\u584a\u7684\u5167\u5bb9\uff0c\u4ee5\u53ca\u5b9a\u7fa9\u4e00\u500b\u61c9\u7528\u7a0b\u5e8f\u7684\u5168\u5c40\u8b8a\u91cf\u3002<\/p>\n<h2>TypeScript \u63a5\u53e3(interfaces)\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>TypeScript \u63a5\u53e3(interfaces)\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: javascript\">\ninterface InterfaceName {\n    propertyName: propertyType;\n    methodName(parameterName: parameterType): returnType;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u8a9e\u6cd5\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <span style=\"color: #009900; font-weight: bold;\">InterfaceName<\/span> \u7684\u63a5\u53e3\uff0c\u5b83\u5305\u542b\u4e00\u500b\u540d\u70ba <span style=\"color: #009900; font-weight: bold;\">propertyName<\/span> \u7684\u5c6c\u6027\uff0c\u5c6c\u6027\u7684\u985e\u578b\u70ba <span style=\"color: #009900; font-weight: bold;\">propertyType<\/span>\uff0c\u4ee5\u53ca\u4e00\u500b\u540d\u70ba <span style=\"color: #009900; font-weight: bold;\">methodName<\/span> \u7684\u65b9\u6cd5\uff0c\u65b9\u6cd5\u7684\u53c3\u6578\u540d\u70ba <span style=\"color: #009900; font-weight: bold;\">parameterName<\/span>\uff0c\u53c3\u6578\u7684\u985e\u578b\u70ba <span style=\"color: #009900; font-weight: bold;\">parameterType<\/span>\uff0c\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u985e\u578b\u70ba <span style=\"color: #009900; font-weight: bold;\">returnType<\/span>\u3002<\/p>\n<h2>TypeScript \u63a5\u53e3(interfaces)\u7684\u5be6\u4f8b<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u4f7f\u7528 TypeScript \u63a5\u53e3(interfaces)\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: javascript\">\ninterface Person {\n    firstName: string;\n    lastName: string;\n    getFullName(): string;\n}\n\nlet person: Person = {\n    firstName: \"John\",\n    lastName: \"Doe\",\n    getFullName(): string {\n        return this.firstName + \" \" + this.lastName;\n    }\n};\n\nconsole.log(person.getFullName()); \/\/ Output: John Doe\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <span style=\"color: #009900; font-weight: bold;\">Person<\/span> \u7684\u63a5\u53e3\uff0c\u5b83\u5305\u542b\u4e86\u5169\u500b\u5c6c\u6027 <span style=\"color: #009900; font-weight: bold;\">firstName<\/span> \u548c <span style=\"color: #009900; font-weight: bold;\">lastName<\/span>\uff0c\u4ee5\u53ca\u4e00\u500b\u540d\u70ba <span style=\"color: #009900; font-weight: bold;\">getFullName<\/span> \u7684\u65b9\u6cd5\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <span style=\"color: #009900; font-weight: bold;\">person<\/span> \u7684\u7269\u4ef6\uff0c\u5b83\u7684\u985e\u578b\u70ba <span style=\"color: #009900; font-weight: bold;\">Person<\/span>\uff0c\u4e26\u4e14\u5be6\u73fe\u4e86\u63a5\u53e3\u4e2d\u5b9a\u7fa9\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u6700\u5f8c\uff0c\u6211\u5011\u8abf\u7528\u4e86 <span style=\"color: #009900; font-weight: bold;\">person<\/span> \u7269\u4ef6\u7684 <span style=\"color: #009900; font-weight: bold;\">getFullName<\/span> \u65b9\u6cd5\uff0c\u8f38\u51fa\u4e86 <span style=\"color: #009900; font-weight: bold;\">John Doe<\/span>\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u63a5\u53e3(interfaces)\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u548c\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u4e2d\u7684\u7269\u4ef6\u548c\u51fd\u6578\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u4eba\u54e1\u66f4\u5bb9\u6613\u5730\u69cb\u5efa\u548c\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u6e1b\u5c11\u51fa\u932f\u7684\u53ef\u80fd\u6027\u3002\u901a\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u4f60\u61c9\u8a72\u5df2\u7d93\u5c0d TypeScript \u63a5\u53e3(interfaces)\u6709\u4e86\u4e00\u5b9a\u7684\u4e86\u89e3\uff0c\u4e26\u4e14\u53ef\u4ee5\u958b\u59cb\u4f7f\u7528\u5b83\u4f86\u958b\u767c\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528TypeScript\u63a5\u53e3(interfaces)\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u6709\u6548\u7387\u5730\u958b\u767c\u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u63a5\u53e3\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u69cb\u5efa\u66f4\u5065\u58ef\u7684\u4ee3\u78bc\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-4996","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-1iA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4996","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=4996"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4996\/revisions"}],"predecessor-version":[{"id":4997,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4996\/revisions\/4997"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}