简体   繁体   中英

How to read a variable from an abstract class?

is it posible to read the "worldInfo" from another class ?

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"

EDIT 1: Ok i created a class in the same package with the 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. It would look something like this:

public String getWorldName(){ this.worldInfo.getWorldName(); }

The class is abstract , so it cannot be initiated. You can read static variables from this class, but you cannot create object of this class. 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.

This variable holds constant? Make it 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)
  • 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).

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