打字机内部的打字机默认功能

Ang*_*gel 16 typescript

这很有效

interface test{
    imp():number;
}
Run Code Online (Sandbox Code Playgroud)

但是可以在里面实现一个功能.

interface test{
    imp():number{
      // do something if it is not overwritten
    }
}
Run Code Online (Sandbox Code Playgroud)

这对我来说不适用于打字稿,但可能有一些保留字,例如默认或类似,用于在接口内实现一个函数,如果默认工作,则不会被覆盖.

bas*_*rat 13

不是.TypeScript接口在运行时不可用,也就是说它们在生成的JavaScript中完全不存在.

也许你打算用class:

class Test{
    imp(){return 123}
 }
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答,我的目的是创建一个由不同类使用的接口,但它们没有逻辑关系,因为使用扩展.所以我使用界面,我查看我的问题,但我什么都没找到,我不知道是否有某种方式.没有问题在每个课程中覆盖/实现相同的功能,只有更舒适,感谢你的时间和你的书. (7认同)
  • 不幸的是,就像许多语言一样,这缺乏多继承性。 (2认同)

Che*_*ain 6

抽象类将满足您的需求

abstract class Department {

    constructor(public name: string) {
    }

    printName(): void {
        console.log("Department name: " + this.name);
    }

    abstract printMeeting(): void; // must be implemented in derived classes
}

class AccountingDepartment extends Department {

    constructor() {
        super("Accounting and Auditing"); // constructors in derived classes must call super()
    }

    printMeeting(): void {
        console.log("The Accounting Department meets each Monday at 10am.");
    }

    generateReports(): void {
        console.log("Generating accounting reports...");
    }
}
Run Code Online (Sandbox Code Playgroud)

学分https://www.typescriptlang.org/docs/handbook/classes.html#:~:text=Abstract%20classes%20are%20base%20classes,methods%20within%20an%20abstract%20class

有关更多信息 https://www.typescriptlang.org/docs/handbook/classes.html#:~:text=Abstract%20classes%20are%20base%20classes,methods%20within%20an%20abstract%20class

  • 类不能多次扩展,作者正在寻找 mixin(但在接口级别)。 (7认同)