Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

class中接口的使用

【implements】用 类 去 实现接口

  • 书写规则

    interface Person {
        name: string;
        sleep(): void;
    }
    
    class Student implements Person {
        constructor(name: string) {
            this.name = name;
        }
        sleep() {}
        study() {}
    }
    
  • 类实现接口时的约束限制

    • 最少要实现接口中的属性,在这基础上可以加其他属性
      • 只能约束公有成员
      • 不能约束构造函数

【extends】用 接口 去 扩展接口

  • 书写规则

    interface Person {
        name: string;
        sleep(): void;
    }
    
    interface Student extends Person {
        study(): void;
    }
    
  • 好处

    • 可以进行任意的组合和复用