ts 中 interface 与 class 的区别

interface: 接口只声明成员方法,不做实现。
class: 类声明并实现方法。
也就是说:interface只是定义了这个接口会有什么,但是没有告诉你具体是什么。
例如:
1
2
3
4
5
interface Point {
  lng: number;
  lat: number;
  sayPosition(): void;
}
Point interface 里面包含数值类型的经纬度和一个sayPosition函数,但是具体内容没有定义,需要你自己在子类中实现。

而class则是完整的实现:
1
2
3
4
5
6
7
8
9
class Point {
  constructor(lng, lat) {
    this.lng = lng;
    this.lat = lat;
  }
  sayPosition() {
    console.log("point:", this.lng, this.lat);
  }
}

.

posted @   每天都要进步一点点  阅读(2352)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示