简体   繁体   English

如何在Java当前类中引用来自其他类的实例

[英]How to refer to an instance from a different class in the current class in Java

I am working on bounding box collision of a laser colliding with a monster in Java. 我正在研究激光与Java中的怪物碰撞的边界碰撞。 There is no problem with collision. 碰撞没有问题。 The problem is the Rectangle object inside the OneEye class is static! 问题是OneEye类中的Rectangle对象是静态的!

Problem 1: This is no problem if one monster was in the game but in my game I will be adding a lot of instances of the Monster class. 问题1:如果游戏中有一个怪物,这没问题,但是在我的游戏中,我将添加很多Monster类的实例。 So my Rectangle object cannot be static if I were to add more than one monster. 因此,如果我要添加多个怪兽,则我的Rectangle对象不能是静态的。

Problem 2: Note: if Rectangle object is non-static my getter method for Rectangle needs be non-static. 问题2:注意:如果Rectangle对象是非静态的,则我的Rectangle的getter方法需要是非静态的。

But I cannot seem to figure how to fix these two problems. 但是我似乎无法弄清楚如何解决这两个问题。

Again, my collision works! 同样,我的碰撞有效! It is more of a code design flaw if I were to add different instances of the same class. 如果要添加同一类的不同实例,则更多是代码设计缺陷。 To sum it all up, how do I refer to an instance from a different class in the current class I am writing code without referring to it in a static context. 综上所述,如何在我编写代码的当前类中引用另一个类中的实例而不在静态上下文中引用它。

Here's my code of two classes: Laser class and OneEye Class 这是我的两个类的代码:Laser类和OneEye类

public class Laser {

/* use a bounding box 
     * to test collision detection
     * 
     */
    private Rectangle rect;

   public void update(long milliseconds) {

// surround a bounding box around the laser
        rect = new Rectangle((int)position.x,(int)position.y,this.getWidth(),this.getHeight());
        // collision against the OneEye monster
        if(rect.intersects(OneEye.getRectangle()))
        {
            Game.getInstance().remove(this);
        }


}

public class OneEye {

/*
     * use bounding box
     * for collision 
     * detection
     */
    private static Rectangle rect;

public void update(long milliseconds) {

        // surround a bounding box around the oneEye monster
        rect = new Rectangle((int)position.x,(int)position.y,this.getWidth(),this.getHeight());
}

// can be useful for collision detection
public static  Rectangle getRectangle()
    {
        return rect;
    }
}

You definitely need to go the no-static route, without a doubt. 毫无疑问,您绝对需要走一条静态路线。 Simply make the rect Rectangle variable and the getRectangle() method non static. 只需将rect Rectangle变量和getRectangle()方法设置为非静态即可。 If this causes further problems, then you will want to post the details of these problems. 如果这引起进一步的问题,则您将需要发布这些问题的详细信息。

Edit You state: 编辑您的状态:

I tried that. 我试过了 But then the problem comes up: I cannot call getRectangle method (which is in my Monster class) in my Laser class. 但是随后出现了问题:我无法在我的Laser类中调用getRectangle方法(在我的Monster类中)。

The problem is not that you can't call getRectangle() in the Laser class, but rather that you can't call it on OneEye,. 问题不在于您不能在Laser类中调用getRectangle() ,而是不能在OneEye上调用它。 something that you've told us little about. 您几乎没有告诉我们的事情。 I was assuming that OneEye is a Monster variable not a Monster subclass. 我以为OneEye是Monster 变量而不是Monster子类。 If the latter, then your design is off as you should be calling methods on class instances, not on the classes themselves. 如果是后者,则您的设计已关闭,因为您应该在类实例上而不是在类本身上调用方法。

Edit 2 编辑2
OK, now that I see OneEye, I see that it is a class, not an instance. 好的,现在我看到了OneEye,我看到它是一个类,而不是实例。 Don't try to call methods off of this. 不要试图从中调用方法。 Instead either create a oneEye variable that holds a Monster object, or a OneEye class that extends Monster, but either way, don't call getRectangle() on a class. 相反,要么创建一个包含Monster对象的oneEye变量,要么创建一个扩展Monster的OneEye类,但是无论哪种方式,都不要在类上调用getRectangle() Call it on a variable that refers to a Monster or Monster child class object. 在引用Monster或Monster子类对象的变量上调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM