0

元の質問

次のシナリオを検討してください。


public abstract class Foo
{
    public string Name { get; set; }

    public Foo()
    {
        this.Name = string.Empty;
    }
    public Foo(string name)
    {
        this.Name = name;
    }
}
public class Bar : Foo
{
    public int Amount { get; set; }

    public Bar()
        : base()
    {
        this.Amount = 0;
    }
    public Bar(string name)
        : base(name)
    {
        this.Amount = 0;
    }
    public Bar(string name, int amount) 
        : base(name)
    {
        this.Amount = amount;
    }
}

それらの間でコードの重複がないように、構築を連鎖させるよりエレガントな手段はありますか? この例では、コードを複製して、Bar.Amountプロパティの値を2 番目のコンストラクターのamountパラメーターの値に設定する必要があります。クラス内の変数の数が増えると、構築のための順列が非常に複雑になる可能性があります。それはちょっと面白いにおいがします。

この問題に関する検索の最初の数ページをふるいにかけましたが、具体的な結果は得られませんでした。これが古い帽子だったらごめんなさい。

前もって感謝します。

アップデート

それで、私はそれについて後ろ向きに考えていました、そして以下が私のアプローチであるべきです:


public abstract class Foo
{
    public string Name { get; set; }
    public string Description { get; set; }

    public Foo()
        : this(string.Empty, string.Empty) { }

    public Foo(string name)
        : this(name, string.Empty) { }

    public Foo(string name, string description)
    {
        this.Name = name;
        this.Description = description;
    }
}
public class Bar : Foo
{
    public int Amount { get; set; }
    public bool IsAwesome { get; set; }
    public string Comment { get; set; }

    public Bar()
        : this(string.Empty, string.Empty, 0, false, string.Empty) { }

    public Bar(string name)
        : this(name, string.Empty, 0, false, string.Empty) { }

    public Bar(string name, int amount)
        : this(name, string.Empty, amount, false, string.Empty) { }

    public Bar(string name, string description, int amount, bool isAwesome, string comment)
        : base(name, description)
    {
        this.Amount = amount;
        this.IsAwesome = isAwesome;
        this.Comment = comment;
    }
}

返信ありがとうございます。

4

4 に答える 4

9

thisはい、キーワードを使用して、C# であるコンストラクターを別のコンストラクターから呼び出すことができます。これは、デフォルト パラメータをシミュレートするためによく使用されます。例:

public class Bar : Foo
{
    public int Amount { get; set; }

    public Bar() : this(String.Empty) {}
    public Bar(string name): this(name, 0) {}
    public Bar(string name, int amount) : base(name)
    {
        this.Amount = amount;
    }
}
于 2009-12-13T17:36:08.870 に答える
3
public class Bar : Foo {
  public int Amount { get; set; } 

  public Bar() : this(null, 0) { }

  public Bar(string name) : this(name, 0) { }

  public Bar(string name, int amount) : base(name){
    this.Amount = amount;    
  }
}
于 2009-12-13T17:40:59.567 に答える
1
public class Bar : Foo {
    public int Amount { get; set; }

    public Bar() : this(0) { }
    public Bar(int amount) : this(String.Empty, amount) { }
    public Bar(string name) : this(name, 0) { }
    public Bar(string name, int amount) : base(name) {
        this.Amount = amount;
    }
}

また

public class Bar : Foo {
    public int Amount { get; set; }

    public Bar() : this(String.Empty, 0) { }
    public Bar(string name) : this(name, 0) { }
    public Bar(string name, int amount) : base(name) {
        this.Amount = amount;
    }
}
于 2009-12-13T17:42:32.700 に答える
-1

コンストラクター チェーンの前提は、実際の作業を行うコンストラクターが 1 つあり、他のコンストラクターがそのコンストラクターにチェーンされていることです。通常は、オプションのパラメーターを設定します。「すべてを行う」コンストラクターは、完全なパラメーターリストを受け取ります。他のコンストラクターは短いパラメーター リストを持ち、連鎖時にデフォルトを渡します。

public class Foo : Bar
{
    private bool whyFoo;

    public Foo() : this(true)
    {
    }
    public Foo(bool why) : base(why, false)
    {
        whyFoo = why;
    }
}
于 2009-12-13T17:37:59.247 に答える