Java: Passing the same objects to each others constructor

前端 未结 6 2332
粉色の甜心
粉色の甜心 2021-01-21 10:56

Evening everyone.

I seem to have hit an odd problem when trying to pass an object to another objects constructor who\'s constructor also relies on the object it\'s being

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-21 11:05

    Mmn.. what you could do is create a model object that wraps around your toolbar and WebPanel.

    public class SomeModel{
      WebPanel panel;
      Toolbar toolbar;
    }
    

    Or you could create your toolbar object.. and in the constructor of the toolbar create your webpanel

    public WebPanel()
    {
       this.toolbar= new Toolbar(this); 
    }
    webPanel = new WebPanel();
    webPanel.getToolbar() ;
    

    Ok that was cheating lmao It depends whether one is a composite object of another; although I think the Model way is better.. no circular reference.

提交回复
热议问题