1 /* 2 1、vscode配置自動編譯 3 4 1.第一步 tsc --inti 生成tsconfig.json 改 "outDir": "./js", 5 6 7 2、第二步 任務 - 運行任務 監視tsconfig.json 8 9 10 2、typeScript中的數據類型 11 12 typescript中為了使編寫的代碼更規范,更有利於維護,增加了類型校驗,在typescript中主要給我們提供了以下數據類型 13 14 15 布爾類型(boolean) 16 數字類型(number) 17 字符串類型(string) 18 數組類型(array) 19 元組類型(tuple) 20 枚舉類型(enum) 21 22 任意類型(any) 23 null 和 undefined 24 void類型 25 never類型 26 27 3、typeScript中的函數 28 29 3.1、函數的定義 30 3.2、可選參數 31 3.3、默認參數 32 3.4、剩余參數 33 3.5、函數重載 34 3.6、箭頭函數 es6 35 4、typeScript中的類 36 37 4.1 類的定義 38 4.2 繼承 39 4.3 類里面的修飾符 40 41 4.4 靜態屬性 靜態方法 42 4.5 抽象類 多態 43 44 45 */ 46 47 48 // 靜態屬性 靜態方法 49 50 /* 51 52 function Person(){ 53 this.run1=function(){ 54 55 } 56 } 57 Person.name='哈哈哈'; 58 59 Person.run2=function(){ 靜態方法 60 61 62 } 63 var p=new Person(); 64 65 Person.run2(); 靜態方法的調用 66 */ 67 68 69 70 /* 71 72 73 function $(element){ 74 75 return new Base(element) 76 } 77 78 $.get=function(){ 79 80 } 81 82 function Base(element){ 83 84 this.element=獲取dom節點; 85 86 87 this.css=function(arr,value){ 88 89 this.element.style.arr=value; 90 } 91 } 92 93 94 95 $('#box').css('color','red') 96 97 98 $.get('url',function(){ 99 100 }) 101 */ 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 // class Per{ 117 // public name:string; 118 // public age:number=20; 119 // //靜態屬性 120 121 // static sex="男"; 122 // constructor(name:string) { 123 // this.name=name; 124 // } 125 // run(){ /*實例方法*/ 126 127 // alert(`${this.name}在運動`) 128 // } 129 // work(){ 130 131 // alert(`${this.name}在工作`) 132 // } 133 // static print(){ /*靜態方法 里面沒法直接調用類里面的屬性*/ 134 135 // alert('print方法'+Per.sex); 136 // } 137 // } 138 139 // // var p=new Per('張三'); 140 141 // // p.run(); 142 143 // Per.print(); 144 145 // alert(Per.sex); 146 147 148 149 150 151 152 //多態:父類定義一個方法不去實現,讓繼承它的子類去實現 每一個子類有不同的表現 153 154 //多態屬於繼承 155 /* 156 157 158 class Animal { 159 160 name:string; 161 constructor(name:string) { 162 this.name=name; 163 } 164 eat(){ //具體吃什么 不知道 , 具體吃什么?繼承它的子類去實現 ,每一個子類的表現不一樣 165 console.log('吃的方法') 166 } 167 } 168 169 class Dog extends Animal{ 170 constructor(name:string){ 171 super(name) 172 } 173 eat(){ 174 175 return this.name+'吃糧食' 176 } 177 } 178 179 180 class Cat extends Animal{ 181 182 constructor(name:string){ 183 super(name) 184 } 185 186 eat(){ 187 188 return this.name+'吃老鼠' 189 } 190 } 191 192 */ 193 194 195 196 //typescript中的抽象類:它是提供其他類繼承的基類,不能直接被實例化。 197 198 199 //用abstract關鍵字定義抽象類和抽象方法,抽象類中的抽象方法不包含具體實現並且必須在派生類中實現。 200 201 202 // abstract抽象方法只能放在抽象類里面 203 204 205 // 抽象類和抽象方法用來定義標准 。 標准:Animal 這個類要求它的子類必須包含eat方法 206 207 208 //標准: 209 210 abstract class Animal{ 211 212 public name:string; 213 constructor(name:string){ 214 215 this.name=name; 216 217 } 218 abstract eat():any; //抽象方法不包含具體實現並且必須在派生類中實現。 219 220 run(){ 221 222 console.log('其他方法可以不實現') 223 } 224 } 225 226 227 // var a=new Animal() /*錯誤的寫法*/ 228 229 230 231 class Dog extends Animal{ 232 233 //抽象類的子類必須實現抽象類里面的抽象方法 234 constructor(name:any){ 235 super(name) 236 } 237 eat(){ 238 239 console.log(this.name+'吃糧食') 240 } 241 } 242 243 var d=new Dog('小花花'); 244 d.eat(); 245 246 247 248 249 class Cat extends Animal{ 250 251 //抽象類的子類必須實現抽象類里面的抽象方法 252 constructor(name:any){ 253 super(name) 254 } 255 run(){ 256 257 258 } 259 eat(){ 260 261 console.log(this.name+'吃老鼠') 262 } 263 264 } 265 266 var c=new Cat('小花貓'); 267 c.eat();
