TS中在抽象类中是否可以写一些已经实现的方法?

TS中在抽象类中是否可以写一些已经实现的方法?

这个代码是一个抽象类BaseEdge,在里面有方法updateCache,这个是已经实现了的。也就是说抽象类中可以有已经实现好的方法是吗?

export abstract class BaseEdge {

  ...
  public updateCache(shapeMap) {
    ['labelShape', 'labelBackgroundShape'].forEach((id) => {
      const shape = shapeMap[id];
      if (shape?.getAttribute(LOCAL_BOUNDS_DIRTY_FLAG_KEY)) {
        this.boundsCache[`${id}Geometry`] = shape.getGeometryBounds();
        this.boundsCache[`${id}Transform`] = shape.style.transform;
        shape.setAttribute(LOCAL_BOUNDS_DIRTY_FLAG_KEY, false);
      }
    });

    const { levelShapes, zoom } = this.zoomCache;
    Object.keys(shapeMap).forEach((shapeId) => {
      const { lod } = shapeMap[shapeId].attributes;
      if (lod !== undefined) {
        levelShapes[lod] = levelShapes[lod] || [];
        levelShapes[lod].push(shapeId);
      }
    });

    const { maxWidth = '60%' } = this.mergedStyles.labelShape || {};
    this.zoomCache.wordWrapWidth = getWordWrapWidthByEnds(
      [this.sourcePoint, this.targetPoint],
      maxWidth,
      1,
    );

    this.zoomCache.zoom = 1;
    this.zoomCache.zoomLevel = 0;
    if (zoom !== 1) this.onZoom(shapeMap, zoom);
  }
}
阅读 1.4k
1 个回答

截屏2023-06-08 20.53.20.png
可以有具体实现的方法,但包含abstract关键字的抽象方法不能有具体实现
截屏2023-06-08 20.55.03.png

推荐问题
logo
Microsoft
子站问答
访问
宣传栏