Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';不要创建对象_Java_Android_Object_Parameters_Constructor - Fatal编程技术网

Java Can';不要创建对象

Java Can';不要创建对象,java,android,object,parameters,constructor,Java,Android,Object,Parameters,Constructor,我有一个包含多个类的应用程序: MenuActivity、MenuThread、MenuView、MenuBot、MenuBall 在“MenuView”类中,我声明了我需要的所有ib对象: this.ball = new MenuBall(this, bot1); this.bot1 = new MenuBot1(this, ball); this.thread = new MenuThread(this,bot1,ball); 如您所见,我还没有创建对象bot1,但我已经将

我有一个包含多个类的应用程序:

MenuActivity、MenuThread、MenuView、MenuBot、MenuBall

在“MenuView”类中,我声明了我需要的所有ib对象:

this.ball = new MenuBall(this, bot1);    
this.bot1 = new MenuBot1(this, ball);    
this.thread = new MenuThread(this,bot1,ball);
如您所见,我还没有创建对象bot1,但我已经将其用作对象球中的参数,这给了我错误

谢谢你试图帮助我

您必须更改(或添加其他)MenuBall和MenuBot1的构造函数。 因此,例如:

public class MenuBall {
    private MenuBot1 menuBot1;
    (...)

    // this constructor doesn't need a MenuBot1 object.
    public MenuBall(MenuView menuView) {
        (...)
    }

    // setter for the menuBot1
    public void setMenuBot1(MenuBot1 menuBot1) {
        this.menuBot1 = menuBot1;
    }

    (...)
}

public class MenuBot1 {
    private MenuBall menuBall;
    (...)

    // this constructor doesn't need a MenuBall object.
    public MenuBot1(MenuView menuView) {
        (...)
    }

    // setter for the menuBall
    public void setMenuBall(MenuBall menuBall) {
        this.menuBall = menuBall;
    }

    (...)
}
然后在MenuView类中:

ball = new MenuBall(this);
bot1 = new MenuBot1(this);
ball.setMenuBot1(bot1);
bot1.setMenuBall(ball);
thread = new MenuThread(this, bot1, ball);
(...)

循环依赖!这样做很糟糕!在
MenuBall
的构造函数中,您想用
bot1
做什么?在创建对象之前不能使用它们-Java无法预测对象将来会是什么样子。@SanPonko Bot1应该改变自己的方向。那么鲍尔就不需要知道这两件事了。但最好将碰撞检测与实体类一起远离。@vakio但Bot1不应改变球的方向。球应。@Tanis.7x Bot1需要获取球的参数,以检测球是否发生碰撞。如果是这样的话,球会改变方向