[英]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()
. 我正在尝试使用
DropPod.getPod()
从另一个类中DropPod.getPod()
另一个类中的类DropPod
的静态方法getPod
,除了我需要一个DropPod.getPod()
参数。 How do I change the getPod
method so I can access it from the other class? 如何更改
getPod
方法,以便可以从其他类访问它?
I know I could just make land() static, but I don't want to do that. 我知道我可以将land()设为静态,但是我不想这样做。 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. 选项1:您可以在其他类中创建DropPod的新实例。 With this instance, you can just call Object.getPod().
使用此实例,您可以只调用Object.getPod()。
Option 2: You already mentioned this, but you could make land a static method as well and DropPod.getPod() should work. 选项2:您已经提到了这一点,但是也可以将land设为静态方法,并且DropPod.getPod()应该可以工作。
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.
如果只需要1个DropPod实例,则可以将其添加为自己类的属性。 Something like a Singleton Pattern .
有点像Singleton模式 。
Add a non-static overload for getPod()
with no parameters, that just calls land()
. 为
getPod()
添加一个无参数的非静态重载, getPod()
重载仅调用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. 很难
getPod()
为什么这个方法实际上确实存在,或者为什么当它不返回任何内容时为什么将其称为getPod()
。 I would remove it and just call land()
directly. 我将其删除,然后直接调用
land()
。
Also hard to see why you want to call a method that calls land()
when you don't have anything to land. 同样很难理解为什么没有东西要登陆时为什么要调用一个调用
land()
的方法。 You need to rethink all this. 您需要重新考虑所有这一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.