1. 类的概念
- 类(Class):定义了一件事物的抽象特点,包含它的属性和方法。
- 对象(Object):类的实例,通过
new生成。 - 面向对象(OOP)的三大特性:封装、集成、多态。
- 封装(Encapsulation):将数据的操作细节隐藏起来,只暴露对外的接口,外接调用端无需知道细节,就能通过对外提供的接口来访问该对象,同时也保证了外接无法任意更改对象内部的数据。
- 继承(Inheritance):子类继承父类,子类除了拥有父类的所有特性之外,还有一些自己的特性。
- 多态(Polymorphism):由继承而产生了相关的不同的类,对同一个方法有不同的响应。比如
Cat和Dog都继承Animal,但是分别实现了自己的eat方法。此时针对某一个实例,我们无需了解他是Cat还是Dog,就可以直接调用eat方法,程序会自动判断出来应该如何执行eat。 - 存取器(
**getter & setter**):用以改变属性的读取和赋值行为。 - 修饰符(
**Modifiers**):修饰符是一些关键字,用于限定成员或者类型的性质。比如public表示公有的属性或方法。 - 抽象类(
**Abstract Class**):抽象类是提供其他类继承的基类,抽象类不允许被实例化。抽象类的方法必须在子类中被实现。 接口(
**Interfaces**):不同类之间公有的属性或者方法,可以抽象成一个接口。接口可以被类实现(Implement)。一个类只能继承自另一个类,但是可以实现多个接口。2. 类的属性和方法
TypeScript中可以使用三种访问修饰符(Access Modifiers),分别是public、private、protected。**public **修饰符的属性或者方法是公有的,可以在任何地方被访问到,默认所有的属性或方法都是public的。**private **修饰符的属性或方法是私有的,不能在声明他的类外部访问。**protected **修饰符的属性或方法是受保护的,它和private类似,区别是它在子类中也是允许被访问的。class Animal {public name: string;constructor(name: string) {this.name = name;}sayHi() {return `My name is ${this.name}`;}static isAnimal(a): boolean {return a instanceof Animal;}}
3. 类的继承
class Cat extends Animal {constructor(name: string) {super(name);console.log('this.name', this.name);}sayHi(): string {return 'Meow, ' + super.sayHi();}}
4. 类的存取器
class ObClass {constructor(name: string) {this.name = name;}public get name(): string {return 'LelandACM';}public set name(v: string) {this.name = v;}}
5. 静态方法
class Animal {public name: string;constructor(name: string) {this.name = name;}sayHi() {return `My name is ${this.name}`;}static isAnimal(a): boolean {return a instanceof Animal;}}
6. 类的用法
```typescript //private 属性 class Animal{ private name :string ; public constructor(name:string){
this.name = name ;
} }
let a = new Animal(‘Jack’) ; console.log(‘a: ‘ + a.name) ; a.name = ‘Tom’ ;
```typescriptclass Animal{private name:string ;public constructor(name:string){this.name = name ;}}class Cat extends Animal{constructor(name:string){super(name) ;console.log('this.name', this.name) ;}}//编译错误- error TS2341: Property 'name' is private and only accessible within class 'Animal'.
class Animal{protected name:string ;public constructor(name:string){this.name = name ;}}class Cat extends Animal{constructor(name:string){super(name) ;console.log('this.name', this.name) ;}}
class Animal{protected name:string ;private constructor(name:string){this.name = name ;}}class Cat extends Animal{constructor(name:string){super(name) ;console.log('this.name', this.name) ;}}let a = new Animal('Jack') ;//编译错误,构造函数为私有时,不可实例化,不可被继承- error TS2675: Cannot extend a class 'Animal'. Class constructor is marked as private.- error TS2673: Constructor of class 'Animal' is private and only accessible within the class declaration.
class Animal{protected name:string ;protected constructor(name:string){this.name = name ;}}class Cat extends Animal{constructor(name:string){super(name) ;console.log('this.name', this.name) ;}}let a = new Animal('Jack') ;//编译错误,当构造函数为protected时,该类只能被继承,不能被实例化- error TS2674: Constructor of class 'Animal' is protected and only accessible within the class declaration.
class Animal {//当存在readonly属性时,相当于在该类中定义该属性,同时给改属性赋值。public constructor(public readonly name: string) {}}let animal = new Animal('Jack');console.log('animal:' + animal.name);
7. 抽象类
abstract class Animal {public name: string;public constructor(name: string) {this.name = name;}public abstract sayHi();}class Cat extends Animal {public sayHi() {console.log(`Hi, I'm ${this.name}`)}}let c = new Cat('tom');c.sayHi();
