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); } } |
.
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步