简体   繁体   English

如果重载构造函数,如何调用super(...)和this(...)?

[英]How to call both super(…) and this(…) in case of overloaded constructors?

I've never needed to do this before but since both have to be the 'first' line in the constructor how should one tackle it? 我以前从未需要这样做,但由于两者都必须是构造函数中的“第一行”,应该如何解决它? What's the best refactoring for a situation like this? 对于这样的情况,最好的重构是什么?

Here's a sample: 这是一个示例:

public class Agreement extends Postable {


public Agreement(User user, Data dataCovered)
{
    super(user);
    this(user,dataCovered,null);

}

public Agreement(User user,Data dataCovered, Price price)
{
    super(user);

    if(price!=null)
        this.price = price;

    this.dataCovered = dataCovered;


}
   ...
}

The call to super(user) is an absolute must. super(user)的调用绝对必须。 How to deal with "optional parameters" in this case? 在这种情况下如何处理“可选参数”? The only way I can think of is repetition ie, don't call this(...) at all. 我能想到的唯一方法就是重复,即根本不要称之为(...)。 Just perform assignments in every constructor. 只需在每个构造函数中执行赋值。

You cannot call both super(..) and this(...). 你不能同时调用super(..)和this(...)。 What you can do is re-work the structure of your overloadeded constructors, so that the last one to be called will call super(...). 你可以做的是重新设计你的overloadeded构造函数的结构,这样最后一个被调用的将调用super(...)。 If that is not an option, you'll have to do the assignments in every constructor. 如果这不是一个选项,您将必须在每个构造函数中进行分配。

If you call this(user,dataCovered,null) , the second constructor will be called, and the first thing it will do is call the super constructor. 如果你调用this(user,dataCovered,null) ,将调用第二个构造函数,它将要做的第一件事就是调用超级构造函数。 So the line super(user); 所以行super(user); in the first constructor is unnecessary. 在第一个构造函数中是不必要的。

In constructors there are (among others like calling not instance methods from or within super or this) those three restrictions: 在构造函数中,有(除了其他类似调用非超级或此内的实例方法)这三个限制:

  • Only one call of super(...) or this(...) per constructor 每个构造函数只能调用一次super(...)this(...)
  • super or this always have to be the first statement in the method super或者this总是必须是方法中的第一个语句
  • If you do not provide any of these you have an implicit super() at the beginning of the constructor 如果您不提供任何这些,则在构造函数的开头有一个隐式的super()

The reason for this is that an object can only be created once. 原因是对象只能创建一次。 And super is creating an object by calling the overloaded constructor while this is delegating to another constructor of the same class. super通过调用重载的构造函数来创建一个对象,而这是委托给同一个类的另一个构造函数。

As another answer is saying in your case you do not need the first super as your this statement is delegating to the other constructor which is already calling super(user) . 正如另一个答案所说,在你的情况下,你不需要第一个super因为你的this语句委托给已经调用super(user)的另一个构造函数。

I would separate out the logic you need to perform in the constructor in a static method in the class, and just call it froum both constructors, to avoid duplication. 我将在类中的静态方法中将构造函数中需要执行的逻辑分离出来,并将其称为两个构造函数,以避免重复。 However, in your case, you can just skip the call to super(user) from the first constructor, and the second one will call it for you :). 但是,在您的情况下,您可以从第一个构造函数中跳过对super(user)的调用,第二个将为您调用它:)。 And I would reverse the "dependencies" between the constructors, like this: 我会颠倒构造函数之间的“依赖关系”,如下所示:

public class Agreement extends Postable {


public Agreement(User user, Data dataCovered)
{
    super(user);
    setDataCovered(dataCovered);

}

public Agreement(User user, Data dataCovered, Price price)
{
    this(user, dataCovered);

    if(price!=null)
        setPrice(price);

}

private static void setDataCovered(Data dataCovered) {
     this.dataCovered = dataCovered;
}

private staitc void setPrice(Price price) {
     this.price = price;
}
}

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

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