类描述了所创建的对象共同的属性和方法。
ts 中定义类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Person { name: string;
constructor(name: string) { this.name = name; }
getName(): string { return this.name; } setName(name: string): void { this.name = name; } }
var p = new Person("张三"); console.log(p.getName()); p.setName("李四"); console.log(p.getName());
|
ts 中实现继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Person { name: string; constructor(name: string) { this.name = name; } run(): string { return `${this.name}在运动`; } }
var p = new Person("杜甫"); console.log(p.run());
class Web extends Person { constructor(name: string) { super(name); } } var w = new Web("白居易"); console.log(w.run());
|
类里面的修饰符
typescript 里面定义属性的时候给我们提供了 三种修饰符:
- public :公有 在当前类里面、 子类 、类外面都可以访问
- protected:保护类型 在当前类里面、子类里面可以访问 ,在类外部没法访问
- private :私有 在当前类里面可以访问,子类、类外部都没法访问
属性如果不加修饰符 默认就是 公有 (public)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Person { public name: string; protected age: number; private sex: string; constructor(name: string, age: number, sex: string) { this.name = name; this.age = age; this.sex = sex; } run(): string { return `${this.name}性别${this.sex},今年${this.age},正在运动`; } }
var p = new Person("杜甫", 18, "男"); console.log(p.name);
console.log(p.run());
class Web extends Person { constructor(name: string, age: number, sex: string) { super(name, age, sex); } run(): string { return `${this.name}今年${this.age},在跳舞`; } } var w = new Web("貂蝉", 88, "女"); console.log(w.run());
|
静态属性和静态方法
es5 中的定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| function Person(name) { this.name = name;
this.run = function () { console.log(this.name); }; }
Person.name = "哈哈哈"; Person.run2 = function () { console.log("静态方法"); };
var p = new Person("张三");
p.run(); Person.run2(); Person.name;
|
ts 中的定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class Person { public name: string;
static sex = "男"; constructor(name: string) { this.name = name; }
run() { console.log(`${this.name}在运动`); } work() { console.log(`${this.name}在工作`); } static print() { console.log("print方法" + Person.sex); } }
var p = new Person("王五"); p.run(); p.work(); Person.print(); console.log(Person.sex);
|
多态
父类定义一个方法不去实现,让继承它的子类去实现 每一个子类有不同的表现。
多态属于继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| 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 + "吃老鼠"; } }
var dog = new Dog("heily"); console.log(dog.eat()); var cat = new Cat("daimo"); console.log(cat.eat());
|
typescript 中的抽象类:它是提供其他类继承的基类,不能直接被实例化。
用 abstract 关键字定义抽象类和抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类中实现。
abstract 抽象方法只能放在抽象类里面
抽象类和抽象方法用来定义标准。
标准:Animal 这个类要求它的子类必须包含 eat 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| abstract class Animal { public name: string; constructor(name: string) { this.name = name; } abstract eat(): any;
run() { console.log("其他方法可以不实现"); } }
class Dog extends Animal { constructor(name: any) { super(name); } eat() { console.log(this.name + "吃粮食"); } }
var d = new Dog("小花花"); d.eat();
|