2017-08-31 29 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を取り外し、BaseLayerクラスでそれprotectedします。この方法で、CellLayerクラスのBaseLayerのメンバーmapServiceにアクセスできます。

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); 
    } 
} 
関連する問題