美文网首页
TypeScript面向对象编程

TypeScript面向对象编程

作者: 杨志聪 | 来源:发表于2023-10-05 11:37 被阅读0次

面向对象编程是编程风格的一种,以对象为基本材料来构建应用程序。对象可以包含数据(属性)和逻辑(方法)。

类和对象

类是创建对象的蓝图。

class Account {
  id: number;
  // 构造方法(constructor)是一个类里面的一个特殊的方法
  // 构造方法会在创建一个实例时被调用
  // 我们在构造方法中初始化属性
  constructor(id: number) { 
      this.id = id;
  }
 }

let account = new Account(1);

访问控制修饰符(Access Modifiers)

有三种访问控制修饰符:public, private和protected。public是默认的修饰符。private和protected的成员都不能被外部访问,但是protected的成员可以被继承,private的成员不能被继承。一般很少会用protected,除非你真的很清楚你要做什么。

class Account {
  // protected的属性可以被继承
  // private的属性不能被继承
  private _balance: number;
  protected _taxRate: number;
}

可选属性和只读属性

class Account {
    // 只读属性
    readonly id: number;
    // 可选属性
    nickname?: string;
    constructor(id: number) {
        this.id = id;
    }
}

参数属性(Parameter Properties)

参数属性(Parameter Properties)允许将构造函数参数直接声明为类的属性。这种语法糖减少了代码的冗余,使得代码更加简洁和清晰。

class Account {
  constructor(public id: number, private _balance: number) {}
}

let a = new Account(1, 100);
console.log(a.id);

getter和setter

getter和setter特性允许我们在读取或者写入属性时,有机会做一些逻辑判断。

class Account {
  constructor(private _balance: number) {}
  get balance() {
    return this._balance;
  }
  set balance(balance: number) {
    if (balance < 0) throw "Invalid balance!";
    this._balance = balance;
  }
}

let a = new Account(100);
a.balance = -1;

索引签名(Index Signatures)

通过索引签名(Index Signatures)可以动态地为一个对象添加属性,同时可以保证类型安全:

class SeatAssignment {
  [seatNumber: string]: string;
}
let seats = new SeatAssignment();
seats.A1 = "Mary";
seats.A2 = "John";

静态方法和属性

class Account {
    static count = 0;
    static foo() {
    }
}

Account.count++;
Account.foo();

继承

class Student extends Person {
}

方法重写(Method Overriding)

class Student extends Person {
  override speak() {
    console.log("Student speaking");
  }
}

抽象类和抽象方法

抽象类,是“不完整”的类。抽象类不能用来实例化对象,需要被继承,在子类中完成完整的类,才能用来实例化对象。

abstract class Shape {
  // 抽象方法不需要写函数体,而且用分号结尾
  abstract render();
}
class Circle extends Shape {
  override render() {
    console.log("Rendering a circle");
  }
}

接口(interface)

我们用接口来定义一个对象的形状。

interface Calendar {
  name: string;
  addEvent(): void;
}
class GoogleCalendar implements Calendar {
    constructor(public name: string) {}
    addEvent(): void {
        
    }
}

Interface VS Type

在TypeScript中,interface和type可以互换使用。

它们都能描述一个对象的形状。

使用type:

type Person = {
  name: string;
};
let tom: Person = { name: "tom" };

使用interface:

interface Person {
  name: string;
}
let tom: Person = { name: "tom" };

它们都能被一个类实现。

使用type:

type Person = {
  name: string;
};

class Student implements Person {
  constructor(public name: string) {}
}

使用interface:

interface Person {
  name: string;
}

class Student implements Person {
  constructor(public name: string) {}
}

相关文章

  • TypeScript面向对象编程

    上面定义了一个类Site,该类有一个方法name,改方法在终端上输出Runoob。 new 关键字创建类的对象,该...

  • TypeScript 面向对象编程

    一、类 面向对象编程中一个重要的核心就是:类,当我们使用面向对象的方式进行编程的时候,通常会首先去分析具体要实现的...

  • Learning TypeScript 读书笔记3

    Chap 4 TypeScript中的面向对象编程 1.面向对象开发的一些原则SOLID: 单一职责原则SRP: ...

  • TS基础

    TypeScript是微软开发的,基于类的面向对象编程,其文件以 .ts 为后缀名; TypeScript是Jav...

  • TypeScript 与面向对象

    面向对象是一种对现实世界理解和抽象的方法。 TypeScript 是一种面向对象的编程语言。 面向对象主要有两个概...

  • TypeScript基础入门教程之三重斜线指令详解

    前言 TypeScript是Javascript的超集,实现以面向对象编程的方式使用Javascript。当然最后...

  • Typescript中private、public、protec

    Typescript中private、public、protected修饰符的区别 什么是面向对象编程? 即Obj...

  • .tsx总结(vue+typescript+less+iview

    TypeScript优势: 可读性 面向对象的编程思想 静态类型检查 代码提示(vscode) 可以使用vue特性...

  • TYPESCRIPT 关于interface理解

    gitbook这篇文档强烈推荐!!!网址:Typescript入门教程 1、面向对象编程三大特性: 继承、多态、封...

  • 面向对象_初识

    目录 面向对象编程介绍 类与对象介绍 私有属性与私有方法 面向对象编程 1. 面向对象编程介绍 面向对象编程:Ob...

网友评论

      本文标题:TypeScript面向对象编程

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