【发布时间】:2018-12-14 16:46:00
【问题描述】:
我有以下课程:
export abstract class LayerStyle {
protected configuration: { [key: string]: string };
protected style: ol.style.Style[];
constructor(config: { [key: string]: string }) {
this.configuration = config;
this.createStyle();
}
protected abstract createStyle(): void;
getStyle() {
return this.style;
}
}
及其子类:
import { LayerStyle } from './LayerStyle';
export class PointStyle extends LayerStyle {
//default values
FILL_COLOR = 'rgba(255,255,255,0.4)';
STROKE_COLOR = '#3399CC';
STROKE_WIDTH = 1.25;
RADIUS = 5;
createStyle() {
let fillColor = this.FILL_COLOR;
let strokeColor = this.STROKE_COLOR;
let strokeWidth = this.STROKE_WIDTH;
let radius = this.RADIUS;
...
}
}
当我创建一个新的 PointStyle let style = new PointStyle(styles).getStyle(); 时,它会说 createStyle() 内的所有类变量 (this.FILL_COLOR,this.STROKE_COLOR,this.STROKE_WIDTH,this.RADIUS) 都为空。为什么?它不应该创建具有其属性的类吗?我应该在子构造函数中初始化它们,然后用super() 调用父级吗?
【问题讨论】:
-
可以添加整个子类代码吗?构造函数不存在,必须是一个
-
嗯。我认为它可以隐式扩展构造函数。
标签: typescript