【问题标题】:How do I use :base() and :this() correctly?如何正确使用 :base() 和 :this() ?
【发布时间】:2012-02-27 01:35:55
【问题描述】:

我已经在 SO 和其他网站上搜索过这个问题,但我还没有找到(或找到)我的案例的解决方案。

我有一个名为EnteBase 的抽象类,我将它用作其他两个类RegioneProvincia 的基础(duh!)。


EnteBase

public abstract class EnteBase
{
    public EnteBase ()
        : this( "Sconosciuto", 0 )
    {
    }

    public EnteBase ( string nome )
        : this( nome, 0 )
    {
    }

    public EnteBase ( string nome, int numeroComuni )
    {
        this.Nome = nome;
        this.NumeroComuni = numeroComuni;
    }


    private string nome;
    public string Nome
    {
        [...]
    }

    private int numeroComuni;
    public int NumeroComuni
    {
        [...]
    }
}

Regione

public class Regione : EnteBase
{
    public List<Provincia> Province
    {
        [...]
    }


    public Regione ()
        : base()
    {
        this.Province = new List<Provincia>();
    }

    public Regione ( string nome )
        : this()
    {
    }

    public Regione ( string nome, int numeroComuni )
        : this()
    {
    }


    public void AggiungiProvincia ( Provincia provincia )
    {
        Province.Add( provincia );
    }
}

Provincia

public class Provincia : EnteBase
{
    private string sigla;

    public string Sigla
    {
        [...]
    }

    public Provincia ()
        : base()
    {
    }

    public Provincia ( string nome )
        : this()
    {
        this.Nome = nome;
    }

    public Provincia ( string nome, int numeroComuni )
        : this()
    {
        this.Nome = nome;
        this.NumeroComuni = numeroComuni;
    }

    public Provincia( string nome, int numeroComuni, string sigla)
        : this()
    {
        this.Nome = nome;
        this.NumeroComuni = numeroComuni;
        this.Sigla = sigla;
    }
}

我的问题如下:

  • 在基类的所有构造函数中使用:this() 是否正确,除了具有最多参数的构造函数,其他指向后者?
  • 使用:this() 指向ProvinciaRegione 类中的基本构造函数,然后从方法本身内部分配给字段是否正确?

我的问题源于我想在每种方法中同时使用:this():base()。当我发现这不可能时,我寻找解决方案,但我找不到一种方法来应用我在 this questionthis one 中看到的内容。

P.S.:在构造函数中,是首选使用this.FieldName还是只使用FieldName

【问题讨论】:

  • 我想你忘了将字段初始化复制到Regione 构造函数中。
  • @hvd 谢谢,我不小心漏掉了

标签: c# constructor this grouping base


【解决方案1】:

使用 :this() 指向 Provincia 和 Regione 类中的基本构造函数然后从方法本身内部分配给字段是否正确?

在 C# 中,对 :base() 的调用是隐式调用,因此不需要显式调用。

因此,在您的示例中,您不需要调用:this(),因为这只是调用基本无参数构造函数。

在基类的所有构造函数中使用 :this() 是否正确,除了具有最多参数的构造函数,其他指向后者?

这是为值提供合理默认值的常用方法,是的。

【讨论】:

  • 既然我已经使用无参数构造函数初始化this.Province,您的第一个答案对Regione 类仍然有效吗?如果没有,应该改变什么?
  • @GabrieleCirulli 您需要保持对:this() 的调用,或者为内联属性初始化一个支持字段。
  • 是平时的private List&lt;Provincia&gt; province = new List&lt;Provincia&gt;()吗?
  • @GabrieleCirulli 是的 - 你明白了!
【解决方案2】:

在基类的所有构造函数中使用:this()是否正确 除了参数最多的那个,其他的都指向 后者?

唯一的用例是构造函数链接,您可以将初始化工作最小化到一个构造函数,并让所有其他构造函数使用 this() 和导致该构造函数执行的参数 - 您已经在 EnteBase 类中执行此操作。

空的base() 调用通常是无用的,因为默认情况下无论如何都会调用默认的基本构造函数。

一个空的this() 调用会走错路,而且通常也无用(除非你有一些不依赖于参数的初始化工作)——你应该传递参数,调用具有更多参数的构造函数,最终结束于完成所有工作的唯一构造函数。

【讨论】:

    【解决方案3】:

    您应该将subclass 中的以下Constructor 更改为:

    public Provincia ( string nome, int numeroComuni )
        : this()
    {
        this.Nome = nome;
        this.NumeroComuni = numeroComuni;
    }
    

    收件人:

    public Provincia ( string nome, int numeroComuni )
        : base(nome, numeroComuni)
    {
    }
    

    ANDConstructor相同,来自:

    public Provincia( string nome, int numeroComuni, string sigla)
        : this()
    {
        this.Nome = nome;
        this.NumeroComuni = numeroComuni;
        this.Sigla = sigla;
    }
    

    收件人:

    public Provincia( string nome, int numeroComuni, string sigla)
        : base(nome, numeroComuni)
    {
        this.Sigla = sigla;
    }
    

    无需在subclass 中设置superclass 的属性,正如superclasses 实现所期望的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多