2016-03-22 5 views
0

StockItem/HeavyStockItem/CarEngine階層の各クラスには2つのコンストラクタが含まれています。C# - StockNumberのインクリメント

StockItemには、最後に使用された在庫番号(10000に初期化)の値を保持する静的フィールドが含まれています。

StockItemのコンストラクタの1つは、このフィールドを無視し、このシステムが実装される前に受け取った在庫を入力するためのパラメータとして在庫番号を受け取ります。 また、DescriptionとCostPriceをパラメータとして受け取ります。

StockItemのコンストラクタの2番目のものは、この静的フィールドを使用して在庫番号を自動的に生成するため、渡された在庫番号引数は必要ありません。 DescriptionとCostPriceのパラメータを受け取ります。

それぞれのStockItemのサブクラスがこのモデルに従い、タスクに適したスーパーコンストラクタを呼び出し、それに応じて(クラスに固有の)追加された引数を処理します。

class StockItem 
{ 

    public static int LastStockNumber = 10000; 
    public int StockNumber; 
    public string Description; 
    public float CostPrice; 

    public StockItem(int StockNumber, string Description, float CostPrice) : this(Description, CostPrice) 
    { 
     this.StockNumber = StockNumber; 
     this.Description = Description; 
     this.CostPrice = CostPrice; 
    } 

    public StockItem(string Description, float CostPrice) 
    { 
     LastStockNumber++; 
     StockNumber = LastStockNumber; 

    } 


class HeavyStockItem : StockItem 
{ 
    private float Weight; 

    public HeavyStockItem(int StockNumber, string Description, float CostPrice, float Weight) : base(StockNumber, Description, CostPrice) 
    { 
     this.Weight = Weight; 
    } 

    public HeavyStockItem(string Description, float CostPrice, float Weight) : base(Description, CostPrice) 
    { 
    } 


class CarEngine : HeavyStockItem 
{ 
    private string EngineNumber; 

    public CarEngine(int StockNumber, string Description, float CostPrice, float Weight, string EngineNumber) : base(StockNumber, Description, CostPrice, Weight) 
    { 
     this.EngineNumber = EngineNumber; 
    } 

    public CarEngine(string Description, float CostPrice, float Weight, string EngineNumber) : base(Description, CostPrice, Weight) 
    {   
    } 

現在、私のコードは、すべてのStockItemのための+1が増加しているが、NO StockNumberが指定された場合、私は唯一の増分にそれをしたいです。

ここの初心者コーダー:

乾杯!

+0

在庫番号は10000,10001,10002などである必要がありますが、現在在庫番号は10000,10002,10004です。 – MeLzA

答えて

0

やりたいことができた後:

class StockItem 
{ 
    //StockItem Variables 
    public static int LastStockNumber = 10000; 
    internal int StockNumber; 
    internal string Description; 
    internal float CostPrice; 


    //Constructor - Ignores static field and receives the StockNumber as a parameter 
    public StockItem(int StockNumber, string Description, float CostPrice) 
    { 
     this.StockNumber = StockNumber; 
     this.Description = Description; 
     this.CostPrice = CostPrice; 
    } 

    //Constructor - Uses this static field to automatically generate a StockNumber and increments +1 to LastStockNumber 
    public StockItem(string Description, float CostPrice) : this(StockItem.LastStockNumber, Description, CostPrice) 
    {   
     this.StockNumber = LastStockNumber; 
     LastStockNumber++; 
     this.CostPrice = CostPrice; 
     this.Description = Description; 
    } 


class HeavyStockItem : StockItem //Extended to StockItem Class 
{ 
    //Variable 
    internal float Weight; 

    //Constructor - Ignores static field and receives the StockNumber and adds Weight to the parameters 
    public HeavyStockItem(int StockNumber, string Description, float CostPrice, float Weight): base(StockNumber, Description, CostPrice) 
    { 
     this.Weight = Weight; 
    } 
    //Constructor 
    public HeavyStockItem(string Description, float CostPrice, float Weight): base(Description, CostPrice) 
    { 
     this.Weight = Weight; 
    } 


class CarEngine : HeavyStockItem //Extended to HeavyStockItem Class 
{ 
    //Variables 
    internal string EngineNumber; 

    //Constructor 
    public CarEngine(int StockNumber, string Description, float CostPrice, float Weight, string EngineNumber): base(StockNumber, Description, CostPrice, Weight) 
    { 
     this.EngineNumber = EngineNumber; 
    } 

    //Constructor 
    public CarEngine(string Description, float CostPrice, float Weight, string EngineNumber): base(Description, CostPrice, Weight) 
    { 
     this.EngineNumber = EngineNumber; 
    } 


私が示唆したように、連鎖コンストラクタに変更を加え、それが正しくインクリメントします。あなたの支援のためにありがとう:)

0

StockItem(int, string, float)コンストラクタから: this(Description, CostPrice)を削除します。

これにより、StockItemベースクラスの別のコンストラクタも呼び出されるため、注入される明示的な在庫番号に関係なくLastStockNumberが常にインクリメントされます。

+0

StockItemを呼び出すときにStockItem.LastStockNumberをインクリメントする必要があります(説明、原価)。したがって、実際にはコンストラクタチェーンの方向を変更することです。 –

+0

@AwakeningByteそれは一つの可能​​性ですが、直接の原因は私の答えで指定した通りです。すべての作成時の増分は、現在のctor連鎖によって発生します。そのための最適な解決策は、OPまで、そしてコードをどのように構造化したいのかということです。私はそのことについて何も仮定していません。 – slugster

+0

在庫番号は10000,10001,10002などである必要がありますが、現在の在庫番号は10000,10002,10004です。 – MeLzA

0

コンストラクタチェーンの方向を変更すると、あなたは私がした結果を得るために、以下の変更を行っ

class StockItem 
{ 

    public static int LastStockNumber = 10000; 
    public int StockNumber; 
    public string Description; 
    public float CostPrice; 

    public StockItem(int StockNumber, string Description, float CostPrice) 
    { 
    this.StockNumber = StockNumber; 
    this.Description = Description; 
    this.CostPrice = CostPrice; 
    } 

    public StockItem(string Description, float CostPrice): this(StockItem.LastStockNumber, Description, CostPrice) 
    { 
     StockItem.LastStockNumber += 1; 
    } 
+0

変更を加えた後、LastStockNumberがデフォルト静的整数intとして10000だけ戻されるようになりました。 – MeLzA

+0

@MeLzA答えを更新しました。 –

+0

LastStockNumberに10000を返し、インクリメントしません。 – MeLzA

関連する問題