【发布时间】:2011-11-21 19:38:09
【问题描述】:
我的BigBlock 类需要一些重载的构造函数。他们都需要以相同的方式初始化相同的几个字段。
这样做的正确方法是什么?是做一个功能吗,例如Initialize在下面的例子中,做这些事情,并且让所有的构造函数调用那个函数?
public class BigBlock {
private Thing parentThing;
Units lengthUnit;
LabCoordinateSystem labCoordinateSystem;
private void Initialize(){
lengthUnit = parentThing.getPreferredUnits(0);
labCoordinateSystem = parentThing.getCoordinateSystem();
}
BigBlock(Thing myThing){
parentThing= myThing;
Initialize();
}
BigBlock(Thing myThing, double x, double y, double z){
parentThing= myThing;
Initialize();
// more code involving x, y, z
}
// a few more constructors
}
【问题讨论】:
标签: java oop constructor