2017-08-31 39 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); 
    } 
}