最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何在TypeScript?中實(shí)現(xiàn)接口的類

 更新時(shí)間:2023年03月27日 14:24:52   作者:跡憶客  
這篇文章主要介紹了TypeScript?中實(shí)現(xiàn)接口的類,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

使用 implements 子句在類中實(shí)現(xiàn)接口,例如 class Developer implements Employee {}。 implements 子句通過定義類的所有屬性和方法來檢查類是否滿足接口。

interface Employee {
  id: number;
  name: string;
  tasks: string[];

  doWork(): void;
}

class Developer implements Employee {
  constructor(
    public id: number, public name: string, public tasks: string[]
   ) {
    this.id = id;
    this.name = name;
    this.tasks = tasks;
  }

  doWork() {
    console.log(`${this.name} writes code`);
  }
}

const dev = new Developer(1, 'Tom', ['develop', 'test', 'ship']);

console.log(dev.name); // ??? "Tom"

TypeScript 中實(shí)現(xiàn)接口的類

我們也可以點(diǎn)擊上面的運(yùn)行示例來查看結(jié)果。

implements 子句允許我們檢查一個(gè)類是否滿足特定的接口。

如果類未能正確實(shí)現(xiàn)接口,則會(huì)發(fā)出錯(cuò)誤。

如果我們的類不希望在初始化時(shí)將特定值作為參數(shù),請(qǐng)使用類屬性。

interface Employee {
  id: number;
  name: string;
  tasks: string[];

  address: {
    country: string;
    city: string;
  };

  doWork(): void;
}

class Developer implements Employee {
  tasks: string[] = ['develop', 'test'];

  address: { country: string; city: string } = {
    country: 'Austria',
    city: 'Linz',
  };

  constructor(public id: number, public name: string) {
    this.id = id;
    this.name = name;
  }

  doWork() {
    console.log(`${this.name} writes code`);
  }
}

const dev = new Developer(1, 'Tom');

console.log(dev.name); // ??? "Tom"

上面的示例直接設(shè)置類屬性,并在構(gòu)造函數(shù)方法中接受參數(shù)。

我們可以使用這種方法來實(shí)現(xiàn)多個(gè)接口。

interface Employee {
  id: number;
  salary: number;
}

interface Person {
  name: string;
}

class Developer implements Employee, Person {
  constructor(
    public id: number, public name: string, public salary: number
  ) {
    this.id = id;
    this.name = name;
    this.salary = salary;
  }
}

const dev = new Developer(1, 'Tom', 100);

console.log(dev.name); // ??? "Tom"

Developer 類實(shí)現(xiàn)了 EmployeePerson 接口。

一個(gè)類可以根據(jù)需要實(shí)現(xiàn)盡可能多的接口。

實(shí)現(xiàn)接口時(shí),我們必須確保在類上設(shè)置所有必要的屬性和方法。

interface Employee {
  id: number;
  salary: number;
}

// ?? Class 'Developer' incorrectly implements interface 'Employee'.
  // Property 'salary' is missing in type 'Developer'
  // but required in type 'Employee'.ts(2420)
class Developer implements Employee {
  constructor(public id: number) {
    this.id = id;
  }
}

TypeScript 中實(shí)現(xiàn)接口的類 Error

Developer 類實(shí)現(xiàn)了 Employee 接口,但沒有定義所需的薪水屬性,因此會(huì)發(fā)出錯(cuò)誤。

我們要么必須將 salary 屬性添加到 Developer 類,要么在接口中將其標(biāo)記為可選。

interface Employee {
  id: number;
  salary?: number; // ??? optional property (can be undefined)
}

class Developer implements Employee {
  constructor(public id: number) {
    this.id = id;
  }
}

salary 屬性被標(biāo)記為可選,因此類不必定義它。

implements 子句所做的就是 - 它檢查類是否滿足特定接口,因此我們必須確保定義所有必需的屬性和方法。

implements 子句的目的只是檢查類是否可以被視為接口類型。

implements 子句不會(huì)更改類或其方法的類型。

interface Employee {
  multiply(a: number, b: number): number;
}

class Developer implements Employee {
  // ?? Error: Parameter 'a' implicitly has an 'any' type.ts(7006)
  multiply(a, b) {
    return a * b;
  }
}

TypeScript error Parameter a implicitly has an any type

盡管該類實(shí)現(xiàn)了為 multiply 函數(shù)定義類型的 Employee 接口,但該類中的 multiply 方法不會(huì)自動(dòng)被類型化。

這是因?yàn)?implements 子句不會(huì)改變類的類型。

interface Employee {
  id: number;
  name?: string; // ??? optional property
}

class Developer implements Employee {
  constructor(public id: number) {
    this.id = id;
  }
}

const dev = new Developer(1);

// ?? Error: Property 'name' does not exist on type 'Developer'.ts(2339)
console.log(dev.name);

typescript error Property name does not exist

如果我們使用可選屬性實(shí)現(xiàn)接口,則不會(huì)在類中自動(dòng)創(chuàng)建它。

我們使用問號(hào)將 Employee 接口中的 name 屬性設(shè)置為可選。

這意味著它可以是字符串或具有未定義的值。

Developer 類正確實(shí)現(xiàn)了 Employee 接口,因?yàn)?name 屬性不是必需的,但是該屬性不會(huì)自動(dòng)分配給該類。

到此這篇關(guān)于TypeScript 中實(shí)現(xiàn)接口的類的文章就介紹到這了,更多相關(guān)TypeScript 接口的類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

奇台县| 石台县| 亚东县| 西宁市| 旬阳县| 闽侯县| 广宗县| 泉州市| 琼海市| 丹阳市| 舒城县| 逊克县| 高唐县| 崇左市| 绥阳县| 阳原县| 瑞金市| 岐山县| 昌乐县| 边坝县| 衡阳县| 永康市| 白银市| 华亭县| 斗六市| 柘城县| 英德市| 仁怀市| 闽清县| 临夏县| 乐都县| 平和县| 临潭县| 古田县| 永吉县| 泽州县| 阜新| 十堰市| 神农架林区| 句容市| 定兴县|