{"id":332,"date":"2022-01-05T15:06:50","date_gmt":"2022-01-05T07:06:50","guid":{"rendered":"http:\/\/gjweb.top\/?p=332"},"modified":"2022-01-05T15:06:51","modified_gmt":"2022-01-05T07:06:51","slug":"2-ts%e4%b8%ad%e7%b1%bb%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=332","title":{"rendered":"2. TS\u4e2d\u7c7b\u7684\u4f7f\u7528"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>\u7c7b\u7684\u57fa\u672c\u4f7f\u7528\u53casuper\u7684\u4f5c\u7528<\/li><li>\u8bbf\u95ee\u7c7b\u578b<\/li><li>\u6784\u9020\u51fd\u6570 constructor<\/li><li>get\u548cset\u5c5e\u6027<\/li><li>\u62bd\u8c61\u7c7b\u548c\u65b9\u6cd5<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7c7b\u7684\u4f7f\u7528\u53casuper\u7684\u4f5c\u7528<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class User{<br> &nbsp; &nbsp;name = \"\u9f9a\u7bad\";<br> &nbsp; &nbsp;getName(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return this.name<br> &nbsp;  }<br>}<br>const xiaojiejie = new User();<br>console.log(xiaojiejie.getName()); \/\/ \u9f9a\u7bads<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">super\u7684\u4f7f\u7528<\/h3>\n\n\n\n<p>super\u7684\u4f5c\u7528\u5728\u4e8e\u8c03\u7528\u7236\u7c7b\u7684constrctor\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528super.xxx \u76f4\u63a5\u8bbf\u95ee\u7c7b\u4e0a\u9762\u7684\u5c5e\u6027<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class User{<br> &nbsp; &nbsp;name=\"\u9f9a\u7bad\";<br> &nbsp; &nbsp;getName(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return this.name<br> &nbsp;  }<br>}<br>class Teacher extends Person{<br> &nbsp; &nbsp;getTeacherNama(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return 'Teacher'<br> &nbsp;  }<br> &nbsp; &nbsp;getName(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return super.getName()+'web'<br> &nbsp;  }<br>}<br>const teacher = new Teacher();<br>console.log(teacher.getName()) \/\/ \u9f9a\u7badweb<br>\u200b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bbf\u95ee\u7c7b\u578b<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>private \u53ea\u5141\u8bb8\u5728\u7c7b\u4e2d\u4f7f\u7528<\/li><li>protected \u53ea\u5141\u8bb8\u5728\u7c7b\u5185\u6216\u8005\u7ee7\u627f\u7684\u5b50\u7c7b\u4e2d\u4f7f\u7528<\/li><li>public \u5728\u5185\u5916\u90fd\u53ef\u4ee5\u4f7f\u7528<\/li><li>static \u76f4\u63a5\u6302\u8f7d\u7c7b\u4e0a\u9762,\u800c\u975e\u5b9e\u4f8b\u4e0a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class User{<br> &nbsp; &nbsp;private &nbsp;name = \"\u9f9a\u7bad\";<br> &nbsp; &nbsp;private sayHi(){<br> &nbsp; &nbsp; &nbsp; &nbsp;console.log(`${this.name} hello`)<br> &nbsp;  }<br>}<br>class Teacher() extends User{<br> &nbsp; &nbsp;sayBye(){<br> &nbsp; &nbsp; &nbsp; &nbsp;this.sayHi()<br> &nbsp;  }<br>}<br>let xxx = new Teacher()<br>xxx.sayBye()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6784\u9020\u51fd\u6570 constructor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person{<br> &nbsp; &nbsp;public name:string;<br> &nbsp; &nbsp;constructor(name:string){<br> &nbsp; &nbsp; &nbsp; &nbsp;this.name = name<br> &nbsp;  }<br>}<br>\/\/ \u7b80\u5199<br>class Person{<br> &nbsp; &nbsp;constructor(public name:string){}<br>}<br>\/\/ \u4f7f\u7528<br>let xxx = new Person(\"\u9f9a\u7bad\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">get\u548cset\u5c5e\u6027<\/h2>\n\n\n\n<p>\u6ce8\u610f getter \u548c setter \u5b9a\u4e49\u7684\u5c5e\u6027\u662f\u4e0d\u7528\u51fd\u6570\u7684\u65b9\u5f0f\u8c03\u7528\u7684,\u53ea\u9700\u8981\u70b9\u64cd\u4f5c\u7b26\u5c31\u53ef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class User{<br> &nbsp; &nbsp;constructor(private name:string){}<br> &nbsp; &nbsp;get getName(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return this.name +\"web\"<br> &nbsp;  }<br> &nbsp; &nbsp;set setName(newName){<br> &nbsp; &nbsp; &nbsp; &nbsp;this.name = newName<br> &nbsp;  }<br>}<br>const person = new Person('\u9f9a\u7bad')<br>console.log(person.getName)<br>person.setName = \"\u5b66\u4e60\"<br>console.log(person.getName)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u62bd\u8c61\u7c7b\u548c\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u62bd\u8c61\u7c7b\u548c\u65b9\u5f0f,\u662f\u4e00\u79cd\u7f16\u7a0b\u601d\u60f3,\u5e76\u4e0d\u662f\u7279\u5b9a\u7684\u8bed\u6cd5<\/p>\n\n\n\n<p>\u672c\u8d28\u7684\u4f5c\u7528\u5c31\u662f\u5c06\u591a\u4e2a\u7c7b\u578b\u4e2d\u540c\u6837\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u62bd\u79bb\u51fa\u6765,\u521b\u5efa\u65b0\u7c7b.\u5e76\u901a\u8fc7\u7ee7\u627f\u7684\u65b9\u5f0f\u4e0b\u653e\u5230\u5404\u4e2a\u5b50\u7c7b\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Person {<br> &nbsp;name: string;<br>}<br>\u200b<br>interface Teacher extends Person {<br> &nbsp;teachingAge: number;<br>}<br>\u200b<br>interface Student extends Person {<br> &nbsp;age: number;<br>}<br>\u200b<br>interface Driver {<br> &nbsp;name: string;<br> &nbsp;age: number;<br>}<br>\u200b<br>const teacher = {<br> &nbsp;name: 'dell',<br> &nbsp;teachingAge: 3<br>};<br>\u200b<br>const student = {<br> &nbsp;name: 'lee',<br> &nbsp;age: 18<br>};<br>\u200b<br>const getUserInfo = (user: Person) =&gt; {<br> &nbsp;console.log(user.name);<br>};<br>\u200b<br>getUserInfo(teacher);<br>getUserInfo(student);<br>\u200b<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7c7b\u7684\u57fa\u672c\u4f7f\u7528\u53casuper\u7684\u4f5c\u7528 \u8bbf\u95ee\u7c7b\u578b \u6784\u9020\u51fd\u6570 constructor get\u548cset\u5c5e\u6027 \u62bd\u8c61\u7c7b\u548c\u65b9\u6cd5  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-332","post","type-post","status-publish","format-standard","hentry","category-typescript"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=332"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/332\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}