如您所见,这将导致问题。代替此方法,可以使用setter-getter方法,在其中使用默认构造函数(例如
ToolBar myToolBar = new ToolBar();
WebPanel webPanel = new WebPanel();
然后使用setter方法设置所需的实例变量,这些变量是完全构造对象所必需的。
myToolBar.setWebPanel(webPanel);
webPanel.setToolBar(myToolBar);
Java:将相同的对象传递给彼此的构造函数
如您所见,这将导致问题。代替此方法,可以使用setter-getter方法,在其中使用默认构造函数(例如
ToolBar myToolBar = new ToolBar();
WebPanel webPanel = new WebPanel();
然后使用setter方法设置所需的实例变量,这些变量是完全构造对象所必需的。
myToolBar.setWebPanel(webPanel);
webPanel.setToolBar(myToolBar);