欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

使用 implements 子句在類中實現(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 中實現(xiàn)接口的類

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

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

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

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

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ù)。

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

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 類實現(xiàn)了 EmployeePerson 接口。

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

實現(xiàn)接口時,我們必須確保在類上設(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 中實現(xiàn)接口的類 Error

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

我們要么必須將 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 子句不會更改類或其方法的類型。

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

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

這是因為 implements 子句不會改變類的類型。

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

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

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

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

Developer 類正確實現(xiàn)了 Employee 接口,因為 name 屬性不是必需的,但是該屬性不會自動分配給該類。

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

相關(guān)文章

  • 詳解微信小程序調(diào)用支付接口支付

    詳解微信小程序調(diào)用支付接口支付

    這篇文章主要介紹了微信小程序調(diào)用支付接口支付,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 純JavaScript實現(xiàn)的分頁插件實例

    純JavaScript實現(xiàn)的分頁插件實例

    這篇文章主要介紹了純JavaScript實現(xiàn)的分頁插件,涉及javascript結(jié)合php動態(tài)實現(xiàn)分頁效果的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • JS實用的帶停頓的逐行文本循環(huán)滾動效果實例

    JS實用的帶停頓的逐行文本循環(huán)滾動效果實例

    下面小編就為大家?guī)硪黄狫S實用的帶停頓的逐行文本循環(huán)滾動效果實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • aspx中利用js實現(xiàn)確認(rèn)刪除代碼

    aspx中利用js實現(xiàn)確認(rèn)刪除代碼

    在一些程序開發(fā)中,對于刪除操作,最好再讓用戶確認(rèn)一下,以免誤操作,帶來的損失,下面的方法,大家可以參考下。各個語言下,都通用的思路。
    2010-07-07
  • 動態(tài)加載JavaScript文件的3種方式

    動態(tài)加載JavaScript文件的3種方式

    第一種是使用document.write/writeln()方式,第二種使用jQuery,第三種是使用原生js方法,感興趣的小伙伴們可以參考一下
    2018-05-05
  • js實現(xiàn)鼠標(biāo)經(jīng)過表格行變色的方法

    js實現(xiàn)鼠標(biāo)經(jīng)過表格行變色的方法

    這篇文章主要介紹了js實現(xiàn)鼠標(biāo)經(jīng)過表格行變色的方法,涉及javascript表格節(jié)點樣式及鼠標(biāo)事件的相關(guān)操作技巧,需要的朋友可以參考下
    2015-05-05
  • js制作網(wǎng)站首頁圖片輪播特效代碼

    js制作網(wǎng)站首頁圖片輪播特效代碼

    這篇文章主要為大家詳細(xì)介紹了js制作網(wǎng)站首頁圖片輪播特效代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • javascript實現(xiàn)的彈出層背景置灰-模擬(easyui dialog)

    javascript實現(xiàn)的彈出層背景置灰-模擬(easyui dialog)

    本文為大家介紹下使用javascript實現(xiàn)的彈出層背景置灰-模擬(easyui dialog) 具體實現(xiàn)如下,感興趣的朋友可以參考下
    2013-12-12
  • webpack如何自動生成網(wǎng)站圖標(biāo)詳解

    webpack如何自動生成網(wǎng)站圖標(biāo)詳解

    這篇文章主要給大家介紹了關(guān)于webpack如何自動生成網(wǎng)站圖標(biāo)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-01-01
  • JavaScript工具庫之Lodash詳解

    JavaScript工具庫之Lodash詳解

    這篇文章主要介紹了JavaScript工具庫之Lodash詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,,需要的朋友可以參考下
    2019-06-06

最新評論