如何在TypeScript?中實(shí)現(xiàn)接口的類
使用 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"
我們也可以點(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)了 Employee 和 Person 接口。
一個(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;
}
}
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;
}
}
盡管該類實(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);
如果我們使用可選屬性實(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)文章
純JavaScript實(shí)現(xiàn)的分頁插件實(shí)例
這篇文章主要介紹了純JavaScript實(shí)現(xiàn)的分頁插件,涉及javascript結(jié)合php動(dòng)態(tài)實(shí)現(xiàn)分頁效果的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
JS實(shí)用的帶停頓的逐行文本循環(huán)滾動(dòng)效果實(shí)例
下面小編就為大家?guī)硪黄狫S實(shí)用的帶停頓的逐行文本循環(huán)滾動(dòng)效果實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
aspx中利用js實(shí)現(xiàn)確認(rèn)刪除代碼
在一些程序開發(fā)中,對(duì)于刪除操作,最好再讓用戶確認(rèn)一下,以免誤操作,帶來的損失,下面的方法,大家可以參考下。各個(gè)語言下,都通用的思路。2010-07-07
動(dòng)態(tài)加載JavaScript文件的3種方式
第一種是使用document.write/writeln()方式,第二種使用jQuery,第三種是使用原生js方法,感興趣的小伙伴們可以參考一下2018-05-05
js實(shí)現(xiàn)鼠標(biāo)經(jīng)過表格行變色的方法
這篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)經(jīng)過表格行變色的方法,涉及javascript表格節(jié)點(diǎn)樣式及鼠標(biāo)事件的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05
javascript實(shí)現(xiàn)的彈出層背景置灰-模擬(easyui dialog)
本文為大家介紹下使用javascript實(shí)現(xiàn)的彈出層背景置灰-模擬(easyui dialog) 具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下2013-12-12
webpack如何自動(dòng)生成網(wǎng)站圖標(biāo)詳解
這篇文章主要給大家介紹了關(guān)于webpack如何自動(dòng)生成網(wǎng)站圖標(biāo)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01

