简体   繁体   中英

How do I call this parameter in another class?

I'm trying to call the static method getPod in the class DropPod from another class with DropPod.getPod() except I need a parameter for DropPod.getPod() . How do I change the getPod method so I can access it from the other class?

I know I could just make land() static, but I don't want to do that. I'd like to try to learn to do it this way.

public class DropPod {

    protected static boolean opened;
    int pos = Random.NormalIntRange(1777, 1794);

    public static void getPod(DropPod drop)
    {
        drop.land();
    } 

    public void land() {
        Level.set(pos, Terrain.DROPPOD_CLOSED);
        Game.updateMap(pos);
        opened = false;
        Dungeon.observe();
    }
}

Option 1: You can create a new instance of DropPod in your other class. With this instance, you can just call Object.getPod().

Option 2: You already mentioned this, but you could make land a static method as well and DropPod.getPod() should work.

Static methods of a class cannot reference non-static methods of its objects. If you only want 1 instance of DropPod, you can add it as a property of it's own class. Something like a Singleton Pattern .

Add a non-static overload for getPod() with no parameters, that just calls land() .

Maybe remove the static version completely. Hard to see why this method exists at all actually, or why it is called getPod() when it doesn't return anything. I would remove it and just call land() directly.

Also hard to see why you want to call a method that calls land() when you don't have anything to land. You need to rethink all this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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