美文网首页
TypeScript(五) Class

TypeScript(五) Class

作者: 梦晓半夏_d68a | 来源:发表于2022-12-26 00:28 被阅读0次

参考

继承

使用 extends 关键字实现继承,子类中使用 super 关键字来调用父类的构造函数和方法。

class Foo {
  name: string;
  age: number;
  constructor(name: string, age: number) {
    this.name = name
    this.age = age
  }
  showName() {
    console.log('name', this.name)
  }
}
let foo11 = new Foo('dan', 12)


// 继承
class Bar extends Foo {
  sex: string
  constructor(name: string, age: number, sex: string) {
    super(name, age)
    this.sex = sex
  }
  showSex() {
    console.log(`sex: ${this.sex}`)
  }
}

let bar = new Bar('bar', 33, 'girl')
console.log(bar.sex, bar.name)
bar.showSex()

描述符

class Foo2 {
  private name: string
  protected sex: string
  public age: number
  constructor(name: string, age: number, sex: string) {
    this.name = name
    this.sex = sex
    this.age = age
  }
  private showName() {
    console.log(`name:${this.name}`)
  }
}

class Bar2 extends Foo2 {
  constructor(name: string, age: number, sex: string ) {
    super(name, age, sex)
  }
  showInfo() {
    // this.name会报错,private 修饰的属性只在父类中使用
    console.log(this.sex, this.age)
  }
}

let foo2 = new Foo2('foo2', 24, 'boy')
// console.log(foo2.name) private 修饰的属性只在父类中使用,子类或者创建的对象都不行
// console.log(foo2.sex) protected 修饰的属性只在父类、子类中使用,创建的对象不行
console.log(`foo2.age: ${foo2.age}`) // public 修饰的属性都可使用,默认方法和属性都是public

let bar2 = new Bar2('bar2', 27, 'boy')
// console.log(bar2.name) 不行
// console.log(bar2.sex) 不行
console.log(bar2.age)
bar2.showInfo()

静态修饰符、只读属性

  • 静态修饰符(static),修饰属性或方法,属于类,只能通过类名调用
  • readonly 修饰的属性,只能读取不能修改
class Foo3 {
  static staticProp: string = '我是静态'
  readonly sex: string
  constructor(sex: string) {
    this.sex = sex
  }
}

let foo3 = new Foo3('boy')
Foo3.staticProp
// foo3.sex = 'girl' 报错

class Bar3 extends Foo3 {
  constructor(sex: string) {
    super(sex)
  }
}
let bar3 = new Bar3('other')
console.log(Bar3.staticProp, Foo3.staticProp)

注意如果 readonly 和其他访问修饰符同时存在的话,需要写在其后面。

class Animal {
  // public readonly name;
  public constructor(public readonly name) {
    // this.name = name;
  }
}

属性简写

class Sports {
   name: string
   age: number
   constructor(name: string, age: number) {
    this.name = name
    this.age = age
  }
}

// 简写
class Sports {
  constructor(public name: string, public number: number) { }
}

抽象类

1、关键字abstract
2、抽象类不允许被实例化,抽象类的存在只为了向子类服务
3、抽象类中包含抽象属性/方法,和普通属性/方法
4、被抽象的属性/方法不允许拥有具体的内容
5、子类如果不是抽象类,就必须将所有抽象父类的方法/属性具体化

abstract class Animal {  // 定义一个抽象类
  abstract name: string // 抽象一个name属性,但是name属性不允许有值,也不允许被 constructor 赋值
  abstract eat(): void// 抽象一个方法,方法不允许有内容,只允许标注返回值类型
  run(): void {  //这是一个普通方法

  }
}
class Dog extends Animal {
  // 因为Dog不是抽象类所以必须有name和eat();第5条
  /*
   只能这样赋值,不允许用constructor(name: string) {
    this.name = name
  }因为父类没有用是抽象的,没用constructor赋予具体内容
  */
  name: string = '狗'
  gender: string
  constructor(gender: string) {
    super()  //此处super中也不允许有父类的抽象属性
    this.gender = gender
  }
  eat(): void {
    console.log('狗吃饭');
  }
}
// const ani: Animal = new Animal() // 报错:无法创建抽象类的实例
const dog: Dog = new Dog('公')
console.log(dog.name);  //狗
dog.eat()

类与接口

一般来讲,一个类只能继承自另一个类,有时候不同类之间会有一些共同的特性,这个时候就可以把共性提取成接口。用 implements 关键字来实现。这个特性大大提高了面向对象的灵活性。
举例来说,门是一个类,防盗门是门的子类。如果防盗门有一个报警器的功能,我们可以简单的给防盗门添加一个报警方法。这时候如果有另一个类,车,也有报警器的功能,就可以考虑把报警器提取出来,作为一个接口,防盗门和车都去实现它:

interface Alarm {
  alert(): void;
}

class Door {
}

class SecurityDoor extends Door implements Alarm {
  alert() {
    console.log('SecurityDoor alert');
  }
}

class Car implements Alarm {
  alert() {
    console.log('Car alert');
  }
}

一个类可以实现多个接口:

interface Alarm2 {
  alert(): any
}
interface Light {
  lightOn(): void;
  lightOff(): void;
}
// 实现多个接口 Car 实现了 Alarm 和 Light 接口,既能报警,也能开关车灯
class Car3 implements Alarm2, Light {
  alert() {
    console.log('Car alert');
  }
  lightOn() {
    console.log('Car light on');
  }
  lightOff() {
    console.log('Car light off');
  }
}

接口继承接口

interface Alarm {
    alert(): void;
}

interface LightableAlarm extends Alarm {
    lightOn(): void;
    lightOff(): void;
}

这很好理解,LightableAlarm 继承了 Alarm,除了拥有 alert 方法之外,还拥有两个新方法 lightOnlightOff

接口继承类

常见的面向对象语言中,接口是不能继承类的,但是在 TypeScript 中却是可以的:

接口继承类

常见的面向对象语言中,接口是不能继承类的,但是在 TypeScript 中却是可以的:

class Point {
    x: number;
    y: number;
    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

interface Point3d extends Point {
    z: number;
}

let point3d: Point3d = {x: 1, y: 2, z: 3};

为什么 TypeScript 会支持接口继承类呢?

实际上,当我们在声明 class Point 时,除了会创建一个名为 Point 的类之外,同时也创建了一个名为 Point 的类型(实例的类型)。

所以我们既可以将 Point 当做一个类来用(使用 new Point 创建它的实例):

class Point {
    x: number;
    y: number;
    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

const p = new Point(1, 2);

也可以将 Point 当做一个类型来用(使用 : Point 表示参数的类型):

class Point {
    x: number;
    y: number;
    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

function printPoint(p: Point) {
    console.log(p.x, p.y);
}

printPoint(new Point(1, 2));

这个例子实际上可以等价于:

class Point {
    x: number;
    y: number;
    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

interface PointInstanceType {
    x: number;
    y: number;
}

function printPoint(p: PointInstanceType) {
    console.log(p.x, p.y);
}

printPoint(new Point(1, 2));

上例中我们新声明的 PointInstanceType 类型,与声明 class Point 时创建的 Point 类型是等价的。

所以回到 Point3d 的例子中,我们就能很容易的理解为什么 TypeScript 会支持接口继承类了:

class Point {
    x: number;
    y: number;
    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

interface PointInstanceType {
    x: number;
    y: number;
}

// 等价于 interface Point3d extends PointInstanceType
interface Point3d extends Point {
    z: number;
}

let point3d: Point3d = {x: 1, y: 2, z: 3};

当我们声明 interface Point3d extends Point 时,Point3d 继承的实际上是类 Point 的实例的类型。

换句话说,可以理解为定义了一个接口 Point3d 继承另一个接口 PointInstanceType

所以「接口继承类」和「接口继承接口」没有什么本质的区别。

值得注意的是,PointInstanceType 相比于 Point,缺少了 constructor 方法,这是因为声明 Point 类时创建的 Point 类型是不包含构造函数的。另外,除了构造函数是不包含的,静态属性或静态方法也是不包含的(实例的类型当然不应该包括构造函数、静态属性或静态方法)。

换句话说,声明 Point 类时创建的 Point 类型只包含其中的实例属性和实例方法:

class Point {
    /** 静态属性,坐标系原点 */
    static origin = new Point(0, 0);
    /** 静态方法,计算与原点距离 */
    static distanceToOrigin(p: Point) {
        return Math.sqrt(p.x * p.x + p.y * p.y);
    }
    /** 实例属性,x 轴的值 */
    x: number;
    /** 实例属性,y 轴的值 */
    y: number;
    /** 构造函数 */
    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
    /** 实例方法,打印此点 */
    printPoint() {
        console.log(this.x, this.y);
    }
}

interface PointInstanceType {
    x: number;
    y: number;
    printPoint(): void;
}

let p1: Point;
let p2: PointInstanceType;

上例中最后的类型 Point 和类型 PointInstanceType 是等价的。

同样的,在接口继承类的时候,也只会继承它的实例属性和实例方法。

相关文章

网友评论

      本文标题:TypeScript(五) Class

      本文链接:https://www.haomeiwen.com/subject/dxmiqdtx.html