[英]How to read a variable from an abstract class?
is it posible to read the "worldInfo" from another class ? 是否可以从另一个类中读取“ worldInfo”?
the following is part of the class that holds it: 以下是包含它的类的一部分:
public abstract class World implements IBlockAccess{
protected WorldInfo worldInfo;
//=====Stuff=====
public World(ISaveHandler par1ISaveHandler, String par2Str, WorldSettings par3WorldSettings, WorldProvider par4WorldProvider, Profiler par5Profiler, ILogAgent par6ILogAgent)
{
this.worldInfo.setWorldName(par2Str);
}
}
i want to use it in my class to get the name. 我想在班上使用它来获取名称。 "worldInfo.getWorldName"
“ worldInfo.getWorldName”
EDIT 1: Ok i created a class in the same package with the World.. "World_Spy.class" 编辑1:好的,我在与World相同的包中创建了一个类。“ World_Spy.class”
public class World_Spy extends World{
public World_Spy(ISaveHandler par1iSaveHandler, String par2Str,
WorldProvider par3WorldProvider, WorldSettings par4WorldSettings,
Profiler par5Profiler, ILogAgent par6iLogAgent) {
super(par1iSaveHandler, par2Str, par3WorldProvider, par4WorldSettings,
par5Profiler, par6iLogAgent);
}
@Override
protected IChunkProvider createChunkProvider() {
return null;
}
@Override
public Entity getEntityByID(int i) {
return null;
}
String TheName = "";
public void gotIt(){
TheName = this.worldInfo.getWorldName();
System.out.println(TheName);
}
}
But when i call it from the main class it crashes the game.. 但是当我从主要班级调用它时,它会使游戏崩溃。
World_Spy WName = new World_Spy(null, null, null, null, null, null);
Is it about the parameters? 关于参数吗?
为了访问worldInfo
您必须扩展World
但是将worldName
设置为World
构造函数的第二个参数,这意味着您必须在子类中知道它,所以..
For the functionality you want, either change the keyword protected to public, or create a public function in the class. 对于所需的功能,可以将关键字protected更改为public,或者在类中创建public函数。 It would look something like this:
它看起来像这样:
public String getWorldName(){ this.worldInfo.getWorldName(); }
The class is abstract
, so it cannot be initiated. 该类是
abstract
,因此无法启动。 You can read static
variables from this class, but you cannot create object of this class. 您可以从此类读取
static
变量,但不能创建此类的对象。 You can make this variabale as static
and then you read it or inherit this class and make object of it or make it non- abstract
and make object of it. 您可以将此可变变量设为
static
,然后读取它或继承该类并使其成为对象,或使其非abstract
并使其成为对象。
This variable holds constant? 这个变量保持不变吗? Make it
static
. 使其
static
。
The field can be accessed directly if one of the following is true: 如果满足以下条件之一,则可以直接访问该字段:
World
(inherited protected fields are visible in derived classes, also World
is not final and has non private constructor) World
(继承的受保护字段在派生类中可见, World
也不是最终的,并且具有非私有构造函数) The field can also be accessed through reflection from any other class after setting accessible property to true on that field (as long as security manager permits). 在该字段上将accessible属性设置为true之后(只要安全管理器允许),还可以通过从任何其他类进行反射来访问该字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.