简体   繁体   English

在Eclipse中更改构造函数模板,以便它调用setter

[英]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.

相关问题 调用firePropertyChange()的setter的Eclipse模板 - Eclipse template for setter that calls firePropertyChange() 更改setter的eclipse模板,以便将实际参数命名为'value'? - Change eclipse template for setter so that actual parameter be named 'value'? 用于getter和setter的Eclipse模板变量 - Eclipse template variable for getter and setter 自定义eclipse模板让setter返回这个对象? - Custom eclipse template to make setter return this object? 基于构造函数参数eclipse生成类变量getter setter - Generate class variable getter setter based on constructor parameters eclipse 在Eclipse中的构造函数模板中设置参数验证? - Set param validation in the constructor template in eclipse? 如何自定义Eclipse的getter和setter生成? - How can I customize Eclipse's getter and setter generation? 无论是调用setter方法还是基于构造函数的注入,使用注释的依赖注入如何在内部工作 - How dependency injection using annotation works internally whether it calls to setter methods or constructor based injection 如何创建与 Guava 的 Optional 一起使用的自定义 Intellij 模板设置器 - How to create a custom Intellij template setter for use with Guava's Optional 构造函数中的Java调用设置程序 - Java call setter in constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM