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

*爱你&永不变心* 提交于 2019-12-22 03:15:15

问题


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. How to deal with "optional parameters" in this case? The only way I can think of is repetition i.e., don't call this(...) at all. Just perform assignments in every constructor.


回答1:


You cannot call both super(..) and 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(...). If that is not an option, you'll have to do the assignments in every constructor.




回答2:


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. So the line super(user); in the first constructor is unnecessary.




回答3:


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 :). 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;
}
}



回答4:


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 or this always have to be the first statement in the method
  • If you do not provide any of these you have an implicit super() at the beginning of the constructor

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.

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).



来源:https://stackoverflow.com/questions/6965561/how-to-call-both-super-and-this-in-case-of-overloaded-constructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!