简体   繁体   English

从另一个构造函数调用构造函数

[英]Calling Constructor from another Constructor

I want to do something like this我想做这样的事情

public class Class1
    {
       public Class1()
       {

       }
       public Class1(int a)
       {

       }
    }
   public class Class2 :Class1
    {
       public Class2(int a)
       {

       }
       public Class2(): base(2)
       {
         this(2);   // new Class2(2);
       }

    }

I know this can't be achieved in Java (can use one between (super or this) in the first line)我知道这无法在 Java 中实现(可以在第一行中使用(super 或 this)之间的一个)

But somehow I am in need of this kind of work how to achieve that?但不知何故,我需要这种工作如何实现呢? Means calling the base class's parameterised and derived class's parameterised constructor in default constructor of derived class.意味着在派生类的默认构造函数中调用基类的参数化和派生类的参数化构造函数。

MSDN article on constructors is pretty good.关于构造函数的MSDN 文章非常好。 Here are some relevant bits:以下是一些相关的位:

A constructor can use the base keyword to call the constructor of a base class.构造函数可以使用 base 关键字来调用基类的构造函数。
.... ....
A constructor can invoke another constructor in the same object using the this keyword.构造函数可以使用 this 关键字调用同一对象中的另一个构造函数。 Like base, this can be used with or without parameters, and any parameters in the constructor are available as parameters to this, or as part of an expression.和 base 一样,this 可以带参数或不带参数使用,构造函数中的任何参数都可以作为 this 的参数,或者作为表达式的一部分。

This should work:这应该有效:

public class Class1
{
   public Class1()
   {

   }
   public Class1(int a)
   {

   }
}
public class Class2 :Class1
{
   public Class2(int a) : base(a)
   {

   }
   public Class2(): this(2)
   {
   }

}

Igor's answer is a fine example of how you should write the constructors in this situation. Igor 的回答是一个很好的例子,说明在这种情况下应该如何编写构造函数。 To address the more general case of your final sentence: you can't chain to more than one constructor.为了解决最后一句话的更一般情况:您不能链接到多个构造函数。 You can't call a base constructor and another constructor in the current class.您不能在当前类中调用基构造函数另一个构造函数。

There are two typical patterns for overloaded constructor.重载构造函数有两种典型的模式。 In the first pattern, the set of overloads of the derived class roughly matches the set of overloads for the base class - you try to make the derived class feel like it's inherited the constructors, effectively.在第一种模式中,派生类的重载集与基类的重载集大致匹配 - 您尝试使派生类感觉它有效地继承了构造函数。 (Constructors themselves aren't inherited, but if you provide the same signatures then it feels like it to the caller.) This is typically the case when your derived class doesn't need additional information. (构造函数本身不会被继承,但是如果您提供相同的签名,那么调用者感觉就像它一样。)当您的派生类不需要附加信息时,通常就是这种情况。 Of course each constructor can have extra parameters, and only pass a subset up to the base constructor, but that can start to get complicated.当然,每个构造函数都可以有额外的参数,并且只将一个子集传递给基本构造函数,但这会开始变得复杂。

In the second pattern, you have several constructors in the derived class each of which calls a "master" constructor in the same (derived) class.在第二种模式中,派生类中有多个构造函数,每个构造函数都调用同一个(派生)类中的“主”构造函数。 This master constructor has the most parameters, as it needs to be able to handle everything specified by any of the other constructors.这个主构造函数的参数最多,因为它需要能够处理任何其他构造函数指定的一切 Sometimes the master constructor should be private, if some combinations wouldn't make sense, but are convenient to specify in one place when you know you can only reach the code via a sensible public constructor.有时主构造函数应该是私有的,如果某些组合没有意义,但是当您知道只能通过合理的公共构造函数访问代码时,可以方便地在一个地方指定。 In this case, only that "master" constructor chains directly to the base class constructor.在这种情况下,只有“主”构造函数直接链接到基类构造函数。 This is typically used when the derived class has several additional pieces of information beyond what the base class needs.这通常用于派生类具有超出基类需要的一些附加信息时。

There are hybrids of this pattern where you have multiple masters with "groups" of overloads calling the masters... but I'd advise you to try to keep it simple where possible.有这种模式的混合体,其中您有多个主控,其中“组”重载称为主控……但我建议您尽可能保持简单。 Also consider the possibility of providing static factory methods instead of constructors - that can end up making for more readable code as you can name the methods by their purpose/parameters - see TimeSpan.FromMinutes for example.还要考虑提供静态工厂方法而不是构造函数的可能性 - 最终可以使代码更具可读性,因为您可以按用途/参数命名方法 - 例如,参见TimeSpan.FromMinutes

Thats impossible in both languages (for a good reason).这在两种语言中都是不可能的(有充分的理由)。 Otherwise you would call the base-constructors multiple times due to the implicit base call if theres no explicit base or this.否则,如果没有显式基或 this,由于隐式基调用,您将多次调用基构造函数。 This could lead to unwanted behaviour.这可能会导致不需要的行为。

Write multiple constructors, and one Init() method编写多个构造函数和一个 Init() 方法

Within each constructor, you can write whatever code you need to execute before passing it to the Init method.在每个构造函数中,您可以编写在将其传递给 Init 方法之前需要执行的任何代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM