一、简介
- 通过
class关键字定义一个类,然后通过new关键字可以方便的生产一个类的实例对象,这个生产对象的过程叫实例化,类的成员就是类中所有的属性和方法。
// 定义类 class Person { // 名称 name: string // 年龄 age: number // 构造函数 constructor(name: string, age: number) { // 更新熟悉数据 this.name = name this.age = age } } // 发送一个人的信息 function sendPerson (person: Person) { console.log(`姓名:${person.name},年龄:${person.age}`) } // 实例化对象 const p = new Person('dzm', 20) sendPerson(p) // 名称:dzm,年龄:20
- 实例在
new出来的时候,它实际是调用了类中的一个方法进行初始化,这个方法被叫做构造器,一般都会在类中显示地写上constructor方法,如果没有显示定义的constructor,就会调用系统自带的constructor构造函数。
二、成员修饰符
- 访问修饰符的作用就是用于限制别人乱用类中的东西
- 访问修饰符
public:公开的,谁都能用(默认就是public)private:私有的,仅类自己能使用,子类与外部都不能使用protected:受保护的,仅类和类的子类能使用,外部不能使用
// 定义类 class Person { // 公开参数 name: string // 公开参数 public age: number // 私有参数 private num:number = 10 // 内部参数 protected num1: number = 20 // 构造函数 constructor(name: string, age: number) { // 更新熟悉数据 this.name = name this.age = age } // 发送个人信息 public send() { console.log('发送成功1') } // 私有方法 private post() { console.log('发送成功2') } }
- 只读修饰符
readonly:只能读不能写
class Person { // 声明赋值 readonly name = 'dzm' } let p = new Person() console.log(p.name) // 不能赋值 // p.name = 'xxx'
readonly只能在constructor构造方法初始化时赋值,或者声明时赋值,之后都不能在修改值。
class Person{ readonly name: string // 构造初始化赋值 constructor(name: string) { this.name = name } }
- 静态修饰符
static:静态成员无需实例化,直接通过类名调用
// 定义类 class Person { // 公开参数 name: string // 公开参数 public age: number // 静态参数 static num:number = 10 // 构造函数 constructor(name: string, age: number) { // 更新熟悉数据 this.name = name this.age = age } // 发送个人信息 static send() { console.log('发送成功') } } // 不需要实例化对象,通过类名就能进行访问 console.log(Person.num) Person.send()
- 总结
1、上面总共分为三种类型修饰符:访问修饰符、只读修饰符、静态修饰符。
2、修饰符是可选的,在没有写任何修饰符的情况下,默认为public。
3、同类型修饰符只能有一个,也就是上面三种修饰符可以组合起来修饰一个成员。
4、三种类型修饰符有先后顺序,分别是:访问、静态、只读,即:public/static/protected、static、readonly
三、实现(implements)
- 类可以被多个接口协议约束,类也可以作为接口使用,也就是
implements后面可以添加单个或多个接口与类。
格式:class 类名 implements 接口名, 接口名, 类名 ... {}
例如:class Person implements Action, Info {} - 案例
// 定义行动接口 interface Action { // 跑起来 run():void } // 定义信息接口 interface Info { // 用户名称 name: string } // 定义一个类,并实现上面的接口 class Person implements Action, Info { // 用户名称 name: string // 跑起来 run(): void { console.log(`${this.name} 跑起来了`) } } // 定义一个类,并实现上面的类接口 class Person2 implements Person { name: string run(): void { console.log(`${this.name} 跑起来了`) } } // 实例化 const p = new Person() p.name = 'DZM' p.run() // 实例化 const p2 = new Person2() p2.name = 'XYQ' p2.run()
四、继承(extends)
- 类不能继承接口协议(
interface),只能通过implements关键词进行实现,支持实现多个接口协议。 - 类不支持多继承,也就是
extends后面只能存在一个父类,但是可以通过多个接口协议来实现多继承。
格式:class 类名 extends 类名 implements 接口名, 接口名, 类名 ... {}
例如:class Person3 extends Person implements Action, Info {} - 接口支持多继承。
格式:interface 接口名 extends 接口名, 接口名, 类名 ... {}
例如:interface Person extends Action, Info {} - 案例
// 定义行动接口 interface Action { // 跑起来 run():void } // 定义信息接口 interface Info { // 用户名称 name: string } // 定义一个类,并实现上面的接口 class Person implements Action, Info { // 用户名称 name: string // 跑起来 run(): void { console.log(`${this.name} 跑起来了`) } } // 定义一个类,并实现上面的类接口 class Person2 extends Person { name: string run(): void { console.log(`${this.name} 跑起来了`) } } // 定义一个类,并实现上面的类接口 class Person3 extends Person implements Action, Info { name: string run(): void { console.log(`${this.name} 跑起来了`) } } // 实例化 const p = new Person() p.name = 'DZM' p.run() // 实例化 const p2 = new Person2() p2.name = 'XYQ' p2.run()