4. TS 中类的使用

70 阅读2分钟
- 类中变量定义
class Circle {
    // ts 中必须先给类声明属性
    public x: number
    public y: number
    constructor(x: number, y: number) {
        this.x = x
        this.y = y
    }
}
// 等同于
class Circle {
    constructor(public x: number, public y: number) {
        this.x = x
        this.y = y
    }
}
- 类中属性类型
class Circle {
    // ts 中必须先给类声明属性
    public x: number
    protected y: number
    private z: number
    constructor(x: number, y: number, z: number) {
        this.x = x
        this.y = y
        this.z = z
    }
}

let circle = new Circle(1, 2, 3)
console.log(circle.x)
console.log(circle.y) // 属性“y”受保护,只能在类“Circle”及其子类 .ts 中访问
console.log(circle.z) // 属性“z”为私有属性,只能在类“Circle”中访问
// 只读属性:只能赋值、读取,不能修改
class Circle {
    constructor(public readonly x: number, public y: number) {
        this.x = x // 构造函数中属于赋值,不属于修改
        this.y = y
    }
    setX(x:number){
        this.x = x // ts 报错:x 是只读属性
    }
}
- 子类重写父类方法时,方法的类型要统一,因为可能在子类方法中调用父类的方法,调用的时候要保证参数一致。
class Animal {
    constructor(public name: string, public age: number) {
        this.name = name
        this.age = age
    }
    eat():string {
        console.log('动物吃')
        return '动物吃'
    }
}
class Cat extends Animal {
    constructor(public name: string, public age: number) {
        super(name, age)
    }
    eat(){
        super.eat()
        console.log('猫吃饭')
        return '猫吃饭' // 子类重写的时候,要兼容父类的类型
    }
}
// 单例
class Singleton {
    private static instance: Singleton;
    private constructor() {
      // 私有构造函数,防止外部实例化
    }
    public static getInstance(): Singleton {
      if (!Singleton.instance) {
        Singleton.instance = new Singleton();
      }
      return Singleton.instance;
    }
    public someMethod(): void {
      console.log("调用了单例类的方法");
    }
  }
  
  // 使用示例
  const instance1 = Singleton.getInstance();
  const instance2 = Singleton.getInstance();
  
  console.log(instance1 === instance2); // true,两个实例是同一个对象
  
  instance1.someMethod(); // 调用单例类的方法

// 抽象类
// 1. 不能被 new
// 2. 抽象类中可以创建抽象属性和方法,让子类来实现,但是静态方法,属性不可以。
// 3. 抽象类中可以有具体的函数实现
abstract class Person {
    public name = "lisi"
    public fun1() {
        return this.name
    }
    public abstract age: number
    public abstract fun2(): string
    abstract static habitat:string = "地球" // static 修饰符不能与 abstract 一起使用
}
abstract class Person {
    public abstract fun1: () => string // 实例方法
    public abstract fun2(): string // 原型方法
}

class Stu extends Person {
    fun1 = () => '123'

    fun2() {
        return '123'
    }
}
// 在 TypeScript 中,方法重载可以根据参数的类型和个数来进行重载,但是参数个数不同的方法重载需要遵循一定的规则。
// 具体来说,参数个数不同的方法重载需要将参数个数较少的重载放在参数个数较多的重载前面。
class Example {
    // 方法重载:参数个数不同的重载
    add(a: number): number;
    add(a: number, b: number): number;
    
    // 实际的方法实现
    add(a: number, b?: number): number {
      if (b) {
        return a + b;
      } else {
        return a;
      }
    }
  }
  
  // 使用示例
  const example = new Example();
  console.log(example.add(1)); // 输出:1
  console.log(example.add(2, 3)); // 输出:5