[英]Change constructor template in Eclipse so it calls setter(s)
For a sample Rectangle class, the auto-generated constructor in Eclipse using code templates is, 对于样本Rectangle类,在Eclipse中使用代码模板自动生成的构造函数是:
public Rectangle (double width, double side) {
this.width = width;
this.height = height;
}
However, assuming setters have been created, I'd like it to be (without having to change it myself) 但是,假设已创建setter,我希望它是(不必自己更改)
public Rectangle (double width, double side) {
setWidth(width);
setHeight(height);
}
any help appreciated! 任何帮助表示赞赏!
Calling an ovevrridable method from constructor is an anti-pattern and that is why eclipse does not support it. 从构造函数调用ovevrridable方法是一种反模式,因此eclipse不支持它。 Reason being the superclass constructor runs before the subclass constructor, so the overriding method in the subclass will be invoked before the subclass constructor has run.
原因是超类构造函数在子类构造函数之前运行,因此子类中的重写方法将在子类构造函数运行之前被调用。 If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.
如果重写方法依赖于子类构造函数执行的任何初始化,则该方法将无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.