2017-08-31 62 views
2

我有下面的类:传递角服务类和基础类

export class CellLayer extends BaseLayer { 

    constructor(name: string, type: LayerType, private mapService: MapService) { 
     super(name, type, mapService); 
    } 
} 

和相应的抽象类:

export abstract class BaseLayer implements ILayer { 

    private _name: string; 
    private _type: LayerType; 

    constructor(name: string, type: LayerType, private mapService: MapService) { 
     this._name = name; 
     this._type = type; 
    } 
} 

全局MapService对象应传递给这两个类。

不过,我现在收到以下错误:

Types have separate declarations of a private property 'mapService'. (6,14): Class 'CellLayer' incorrectly extends base class 'BaseLayer'.

回答

4

使其受到保护。

Private表示属性对于当前类是私有的,因此子组件不能覆盖它,也不能定义它。

export abstract class BaseLayer implements ILayer { 

    private _name: string; 
    private _type: LayerType; 

    constructor(name: string, type: LayerType, protected mapService: MapService) { 
     this._name = name; 
     this._type = type; 
    } 
} 
export class CellLayer extends BaseLayer { 

    constructor(name: string, type: LayerType, protected mapService: MapService) { 
     super(name, type, mapService); 
    } 
} 
5

CellLayer构造函数删除private,并使其protectedBaseLayer类。通过这种方式,您可以访问CellLayer类中的BaseLayermapService成员。

export abstract class BaseLayer implements ILayer { 

    private _name: string; 
    private _type: LayerType; 

    constructor(name: string, type: LayerType, protected mapService: MapService) { 
      this._name = name; 
      this._type = type; 
    } 
} 

export class CellLayer extends BaseLayer { 

    constructor(name: string, type: LayerType, mapService: MapService) { 
     super(name, type, mapService); 
    } 
}