typeScript面试必备之-通识七:typeScript中的可索引接口(数组,对象)+类类型接口...
生活随笔
收集整理的這篇文章主要介紹了
typeScript面试必备之-通识七:typeScript中的可索引接口(数组,对象)+类类型接口...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
可索引接口:數組、對象的約束 (不常用)
ts定義數組的方式
1.1可索引接口 對數組的約束
interface UserArr{[index:number]:string//表示定義的數組是key是number類型,value是string類型}var arr:UserArr=['aaa','bbb'];console.log(arr[0]);// var arr:UserArr=[123,'bbb']; /*錯誤*/// console.log(arr[0]); 復制代碼1.2可索引接口 對對象的約束
interface UserObj {[index: string]: string } var arr: UserObj = { name: '張三' }; 復制代碼1.3類類型接口:對類的約束 和 抽象類抽象有點相似 類類型接口中定義的不管是屬性還是方法都要在子類中實現否則會報錯!!!
interface Animal{name:string;eat(str:string):void; } class Dog implements Animal{name1:string;constructor(name:string){this.name1=name;}eat(){console.log(this.name1+'吃糧食')} } 復制代碼正確寫法
class Cat implements Animal{name:string;constructor(name:string){this.name=name;}eat(food:string){console.log(this.name+'吃'+food);} } 復制代碼感謝: 參考學習:https://www.itying.com:
自己總結到有道云筆記里面有更加清楚的標注方便查閱:
* Typescript 介紹
http://note.youdao.com/noteshare?id=c3e0d5810580041ade028a345519ce96&sub=E882CE350AC94C5596B146908C406274 * typeScript中的數據類型
http://note.youdao.com/noteshare?id=a07dd9a55779068d9409b79014a9a409&sub=CE3DA74B1A27412792DF9BB8F74E8E24 * typeScript中的函數
http://note.youdao.com/noteshare?id=adc64e95404f3c382c7549f05841c7b2&sub=65D365DE900049A28DD4697B61A5544A * 對象+繼承
http://note.youdao.com/noteshare?id=7f82be21375761c13ccad1981510a614&sub=7856743A59934A59A1AA608AA78497F0 * 類的定義、繼承、類里面的修飾符 http://note.youdao.com/noteshare?id=a4420ac729d8cc7c1eb72fc47198f00b&sub=399974DBD9FA4EC2A99548D8837CB61F * 靜態屬性 靜態方法 抽象類多態
http://note.youdao.com/noteshare?id=efad91320514e4f21e07ec77a9bcfd7c&sub=DC3868B8459940CA884DC5B80EE25443
* typeScript中的接口
http://note.youdao.com/noteshare?id=ae4c7f13bec8e35b3fda4e326d7e7055&sub=6001FADE75904080A15395886608D366 * 可索引接口(數組,對象)+類類型接口
http://note.youdao.com/noteshare?id=aebf26ab922044a69cae082866ce8d86&sub=F73FB04DD10C4BA796173FBE40280B21
* 接口的繼承、接口的擴展
http://note.youdao.com/noteshare?id=93e1dff481b8e299ee9c925e8e3a8b4a&sub=3C96456BFAA34C28BA6BB4E0B0C1E77B * 泛類
http://note.youdao.com/noteshare?id=6d9e0ec4cdb17249bf1bb11fae2e44ec&sub=B0C47B2089AC42E48C2E1BAAAD3FE9EF * 泛型接口
http://note.youdao.com/noteshare?id=49af2ba1bd6173467e7c7b844def959b&sub=F16F0C2857E347C0B8FB69522D5DEFA2
總結
以上是生活随笔為你收集整理的typeScript面试必备之-通识七:typeScript中的可索引接口(数组,对象)+类类型接口...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 提高mysql千万级大数据SQL查询优化
- 下一篇: 区块链2.0:智能合约