typescript學習之旅----靜態屬性 靜態方法 抽象類 多型
阿新 • • 發佈:2018-12-17
// 靜態屬性 靜態方法 /* function Person(){ this.run1=function(){ } } Person.name='哈哈哈'; Person.run2=function(){ 靜態方法 } var p=new Person(); Person.run2(); 靜態方法的呼叫 */ // class Per { // public name: string; // public age: number = 20; // //靜態屬性 // static sex = "男"; // constructor(name: string) { // this.name = name; // } // run() { /*例項方法*/ // alert(`${this.name}在運動`) // } // work() { // alert(`${this.name}在工作`) // } // static print() { /*靜態方法 裡面沒法直接呼叫類裡面的屬性*/ // alert('print方法' + Per.sex); // } // } // // var p=new Per('張三'); // // p.run(); // Per.print(); // alert(Per.sex); //多型:父類定義一個方法不去實現,讓繼承它的子類去實現 每一個子類有不同的表現 //多型屬於繼承 // class Animal { // name: string; // constructor(name: string) { // this.name = name; // } // eat() { //具體吃什麼 不知道 , 具體吃什麼?繼承它的子類去實現 ,每一個子類的表現不一樣 // console.log('吃的方法') // } // } // class Dog extends Animal { // constructor(name: string) { // super(name) // } // eat() { // return this.name + '吃糧食' // } // } // class Cat extends Animal { // constructor(name: string) { // super(name) // } // eat() { // return this.name + '吃老鼠' // } // } //typescript中的抽象類:它是提供其他類繼承的基類,不能直接被例項化。 //用abstract關鍵字定義抽象類和抽象方法,抽象類中的抽象方法不包含具體實現並且必須在派生類中實現。 // abstract抽象方法只能放在抽象類裡面 // 抽象類和抽象方法用來定義標準 。 標準:Animal 這個類要求它的子類必須包含eat方法 //標準: abstract class Animal { public name: string; constructor(name: string) { this.name = name; } abstract eat(): any; //抽象方法不包含具體實現並且必須在派生類中實現。 run() { console.log('其他方法可以不實現') } } // var a=new Animal() /*錯誤的寫法*/ class Dog extends Animal { //抽象類的子類必須實現抽象類裡面的抽象方法 constructor(name: any) { super(name) } eat() { console.log(this.name + '吃糧食') } } var d = new Dog('小花花'); d.eat(); class Cat extends Animal { //抽象類的子類必須實現抽象類裡面的抽象方法 constructor(name: any) { super(name) } run() { console.log(this.name) } eat() { console.log(this.name + '吃老鼠') } } var c = new Cat('小花貓'); c.eat();