简体   繁体   English

如何从抽象类中读取变量?

[英]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

Actually, protected means it can be used by childclasses but also by any other class in the same package. 实际上, 保护意味着它可以由子类使用,也可以由同一包中的任何其他类使用。 So yes, you can use it by all classes in the same package even if they're not subclassing the abstract class 所以是的,即使它们没有继承抽象类,您也可以在同一包中的所有类中使用它

The field can be accessed directly if one of the following is true: 如果满足以下条件之一,则可以直接访问该字段:

  • That another class extends World (inherited protected fields are visible in derived classes, also World is not final and has non private constructor) 另一个类扩展了World (继承的受保护字段在派生类中可见, World也不是最终的,并且具有非私有构造函数)
  • That another class belongs to the same package (protected fields are visible in classes from the same package, same as package private). 另一个类属于同一程序包(受保护的字段在同一程序包的类中可见,与程序包私有相同)。

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.

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