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属性的值设置为第二个构造函数中的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

构造函数链接的假设是有一个构造函数做真正的工作,而另一个构造函数链接到那个。通常是设置可选参数;“做这一切”的构造函数采用完整的参数列表。其他构造函数具有较短的参数列表,并在链接时传递默认值。

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 回答