0
点赞
收藏
分享

微信扫一扫

Typescript的类


文章目录

  • ​​类定义​​
  • ​​成员可访问性​​
  • ​​public​​
  • ​​protected​​
  • ​​private​​
  • ​​私有字段​​
  • ​​继承​​
  • ​​派生类实例化​​
  • ​​实现接口​​
  • ​​静态成员​​

JavaScript的类本质上是函数,而Typescript是对JavaScript类进行扩展,添加类型的支持,比如实现接口、泛型类等等。

类定义

  1. 类声明,语法:

class ClassName {
// ...
}

ClassName是类名,是必选的,而且首字母是大写。
2. 类表达式,语法:

const Circle = class {
name: number;
constructor() {
this.name = 897;
}
}

const a = new Circle();
console.log(a.name)

类的成员变量最好是设置初始值,这样避免因为没有初始值引发的错误。在tsconfig.json文件中启动​​strictPropertyInitialization​​​编译选项来帮助严格检查成员变量是否赋值,但是它需要和​​strictNullChecks​​选项同时启动才生效。

"strictNullChecks": true, 
"strictPropertyInitialization": true,

Typescript的类_typescript

成员可访问性

Typescript类提供了三种可访问性修饰符:public、protected和private。

public

类的公共成员没有访问限制,但是可以在类的内部、外部以及派生类的内部访问,公有成员是用public修饰符来标识的,比如:

const Circle = class {
public name: number = 90;
age: number; // 在构造函数中初始化
type: string;
constructor() {
this.age = 897;
this.type = ''
}
}

类在默认的情况下,所有成员都是公有成员,所以可以省略public修饰符。

protected

类的受保护成员,允许在当前类内部和派生类内部可以访问,但是不能在类外部访问,比如:

const Circle = class {
protected name: number = 90;
age: number; // 在构造函数中初始化
type: string;
constructor() {
this.age = 897;
this.type = '';
}
}

const data = new Circle()
console.log(data.name)

Typescript的类_构造函数_02

private

类的私有成员,允许在当前类的内部访问,在外部或者派生类内部都不能访问,比如:

class Circle {
private x: number = 90;
test() {
this.x;
}
}

class Derived extends Circle {
out() {
this.x;
}
}

Typescript的类_前端_03

私有字段

类的私有字段,可以使用"#"来表示,比如:

class Circle {
#x: number = 90;
test() {
this.#x;
}
}

class Derived extends Circle {
out() {
this.#x; //
}
}

Typescript的类_javascript_04

继承

类的继承,是通过关键字extends来指定要继承的类,语法:

class DerivedClass extends BaseClass { }

继承下来的子类,也叫作派生类,比如:

class Shape {
color: string = 'red';

changeColor() {
return this.color = this.color === 'red' ? 'white' : 'black';
}
}

class DerivedShape extends Shape { }

const derivedShape = new DerivedShape();
console.log(derivedShape.changeColor())

继承之后,可以在子类中重写父类的成员:

class Shape {
color: string = 'red';

changeColor() {
return this.color = this.color === 'red' ? 'white' : 'black';
}
}

class DerivedShape extends Shape {
color: string='blue';
}

const derivedShape = new DerivedShape();
console.log(derivedShape.changeColor())

派生类实例化

在派生类的构造函数中,必须调用父类的构造函数,不然不能正确实例化派生类。

在派生类的构造函数中使用super()语句就能够调用父类的构造函数,比如:

class Shape {
color: string;
constructor() {
this.color = 'red';
}
changeColor() {
return this.color = this.color === 'red' ? 'white' : 'black';
}
}

class DerivedShape extends Shape {
radius:number;
constructor(){
super();
this.radius = 90
}
}

如果派生类中定义了构造函数,但是没有添加super语句,就会编译错误:

Typescript的类_派生类_05


子类初始化的顺序是:

  1. 初始化父类的属性
  2. 调用父类的构造函数
  3. 初始化子类的属性
  4. 调用父类的构造函数

实现接口

一个类只能允许继承一个父类,但是可以实现多个接口,在定义的时候,使用implement语句来声明类所实现的接口,如果是实现多个接口的话,就使用逗号分开。比如:

interface A {
name: string;
}
interface B {
sum: number;
}

class C implements A, B {
name = '32';
sum;
constructor() {
this.sum = 908;
}
}

静态成员

类的静态成员不属于某个实例,而是类的本身,静态成员是通过static关键字来定义的,只能通过类名来访问,比如:

class D{
static version:string = '1.0';
}
const d = new D();
console.log(D.version);//1.0
console.log(d.version);// 编译报错

报错内容:

Typescript的类_构造函数_06

这就类的基本知识


举报

相关推荐

0 条评论