开发者

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.开发者_StackOverflow中文版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.


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.


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.


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;
}
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜