2017-03-09 4 views
0

これは相対的に新しいので、私はtypescriptを使用して依存性注入を実装したいのですが、私はこのパターンを使用しています 私はインターネット上の例を見つけたので、EclipseやVisual Studioで問題なく使用できますが、typescriptでIDEを使用するとエラーが発生しますtypescriptを使用して依存性注入を使用する方法はありますか

Supplied parameters do not match any signature of call target 

と、このエラーが

表示されたときにちょうどの終わりにそれを実装されています。このような

私の基本クラス:エラーが表示される場所

class Motor { 
    Acelerar(): void { 
    } 
    GetRevoluciones(): number { 
     let currentRPM: number = 0; 
     return currentRPM; 
    } 
} 
export {Motor}; 

私のクラスここではモーター

import { Motor } from "./1"; 
class Vehiculo { 
    private m: Motor; 
    public Vehiculo(motorVehiculo: Motor) { 
     this.m = motorVehiculo; 
    } 
    public GetRevolucionesMotor(): number { 
     if (this.m != null) { 
      return this.m.GetRevoluciones(); 
     } 
     else { 
      return -1; 
     } 
    } 
} 
export { Vehiculo }; 

私のインターフェイスとモーター

interface IMotor { 
    Acelerar(): void; 
    GetRevoluciones(): number; 
} 
class MotorGasoline implements IMotor { 
    private DoAdmission() { } 
    private DoCompression() { } 
    private DoExplosion() { } 
    private DoEscape() { } 
    Acelerar() { 
     this.DoAdmission(); 
     this.DoCompression(); 
     this.DoExplosion(); 
     this.DoEscape(); 
    } 
    GetRevoluciones() { 
     let currentRPM: number = 0; 
     return currentRPM; 
    } 
} 
class MotorDiesel implements IMotor { 
    Acelerar() { 
     this.DoAdmission(); 
     this.DoCompression(); 
     this.DoCombustion(); 
     this.DoEscape(); 
    } 
    GetRevoluciones() { 
     let currentRPM: number = 0; 
     return currentRPM; 
    } 
    DoAdmission() { } 
    DoCompression() { } 
    DoCombustion() { } 
    DoEscape() { } 
} 

の種類を使用し、次のとおりです。

import { Vehiculo } from "./2"; 
enum TypeMotor { 
    MOTOR_GASOLINE = 0, 
    MOTOR_DIESEL = 1 
} 
class VehiculoFactory { 
    public static VehiculoCreate(tipo: TypeMotor) { 
     let v: Vehiculo = null; 
     switch (tipo) { 
      case TypeMotor.MOTOR_DIESEL: 
       v = new Vehiculo(new MotorDiesel()); break; 
      case TypeMotor.MOTOR_GASOLINE: 
       v = new Vehiculo(new MotorGasoline()); break; 
      default: break; 
     } 
     return v; 
    } 
} 

私はしたいが、SIMPLE-DIJSまたはD4jsや一瞬のために他のどのような任意のライブラリやモジュールを使用していない、私はちょうどたいそれらなしで実装する方法を知っている

答えて

0

あなたが指定されていないため、このエラーが発生していますVehiculoタイプのコンストラクタです。

コンストラクタを宣言するには、クラス名ではなくconstructorキーワードを使用する必要があります。 、作品のおかげで、しかし、あなたは「自動車ガソリン」や「モーター電気」よりも仮定クラスファクトリにパラメータを置く方法のアイデアを持っています

class Vehiculo { 
    private m: Motor; 
    constructor(motorVehiculo: Motor) { 
     this.m = motorVehiculo; 
    } 
    public GetRevolucionesMotor(): number { 
     if (this.m != null) { 
      return this.m.GetRevoluciones(); 
     } 
     else { 
      return -1; 
     } 
    } 
} 
+0

はそれが私はそれを取得しない独自のコンストラクタ – Lrawls

+0

のしています。コードサンプルで質問を更新して何をしたいですか? –

+0

忘れてしまった – Lrawls

関連する問題