代码之家  ›  专栏  ›  技术社区  ›  Caleb

从静态上下文引用的非静态方法[重复]

  •  -2
  • Caleb  · 技术社区  · 9 年前

    我有两个相同的错误,如下所示:

    class FBox {//...}
    class FBPlayer
    {
        //Initialized instances
        FBox game = new FBox();
        **FBPillar pillar = new FBPillar();**
        **FBObjects objects = new FBObjects();**
        //Lots o Properties...
    
        public boolean get_Alive() { return this.b_PlayerAlive; }
        public void set_Alive(boolean alive) { this.b_PlayerAlive = alive; }
    
        //My Error ridden Method
        public void checkCollision()
        {
            if(get_YPos() >= **objects**.get_Ground())
                              ^My Error was incorrect name for my instance
            {
                set_Alive(false);
            }
            else if(get_Bounds().intersects(**pillar**.get_Bounds()))   
                                    ^My Error was incorrect name for my instance
            {
                set_Alive(false);
            } 
        }
    
    class FBPillar
    {
        public int get_Bounds() {return 'the variable'; }
    }
    
    class FBObjects
    {
        public int get_Ground() {return 'the variable'; }
    }
    

    错误出现在if语句和else if语句中 当我运行它时,它返回错误:

    FBox.java:178: error: non-static method get_Bounds() cannot be referenced from a static context
                   else if(get_Bounds().intersects(**FBPillar**.get_Bounds()))
    

    if语句的错误相同,但使用FBObjects.get_Ground()) ^

    1 回复  |  直到 9 年前
        1
  •  0
  •   200_success    9 年前

    你说的是谁的界限?你可能是说

    if (get_Bounds().intersects(pillar.get_Bounds())) {
        …
    }
    

    我还要补充一点

     FBPlayer player = new FBPlayer();
    

    意思是一个玩家包含一个玩家,这可能不是你想要的。