TypeScript极速梳理


6.抽象类

<p>常规类:</p> <pre><code>class Person { name: string age: number constructor(name:string,age:number){ this.name = name this.age = age } } const p1 = new Person('张三',18) const p2 = new Person('李四',19) console.log(p1) console.log(p2)</code></pre> <p>继承:</p> <pre><code>// Person类 class Person { } // Teacher类继承Person class Teacher extends Person { } // Student类继承Person class Student extends Person { } // Person实例 const p1 = new Person('周杰伦',38) // Student实例 const s1 = new Student('张同学',18) const s2 = new Student('李同学',20) // Teacher实例 const t1 = new Teacher('刘⽼师',40) const t2 = new Teacher('孙⽼师',50)</code></pre> <p>抽象类:不能去实例化,但可以被别⼈继承,抽象类⾥有抽象⽅法</p> <pre><code>// Person(抽象类) abstract class Person { } // Teacher类继承Person class Teacher extends Person { // 构造器 constructor(name: string,age: number){ super(name,age) } // ⽅法 speak(){ console.log('你好!我是⽼师:',this.name) } } // Student类继承Person class Student extends Person { } // Person实例 // const p1 = new Person('周杰伦',38) // 由于Person是抽象类,所以此处不可以new Perso n的实例对象</code></pre>

页面列表

ITEM_HTML