TypeScript 类接口
示例
public在接口中声明变量和方法,以定义其他打字稿代码如何与之交互。
interface ISampleClassInterface {
sampleVariable: string;
sampleMethod(): void;
optionalVariable?: string;
}在这里,我们创建一个实现接口的类。
class SampleClass implements ISampleClassInterface {
public sampleVariable: string;
private answerToLifeTheUniverseAndEverything: number;
constructor() {
this.sampleVariable= 'string value';
this.answerToLifeTheUniverseAndEverything= 42;
}
public sampleMethod(): void {
//没做什么
}
private answer(q: any): number {
return this.answerToLifeTheUniverseAndEverything;
}
}该示例说明如何创建一个接口ISampleClassInterface和一类SampleClass是implements接口。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短