简体   繁体   中英

How can I reference a static class with a separate enum class in Java?

This is my current enum class:

public enum BuildingType {
MINER("Miner", "A basic slave." (*, Miner (separate class)*)), FARM("Farm", "Old Macdonald's Crib."), BAKERY("Bakery", "Best Cookies in Town!"), FACTORY("Factory", "Highest Quality Memes in town!"), QUARRY("Quarry", "Let's get drilling!");

private String name;
private String description;
//private Class clazz;

BuildingType(String name, String description (* Possible to put Class clazz reference here? *)) {
    this.name = name;
    this.description = description;
    //this.clazz = clazz
}

public String getName() {
    return name;
}

public String getDescription() {
    return description;
}

public Class getReferencedClass() {
    //Return referenced "clazz" above
}
}

As put in the comments and (* *), is it possible to reference a static class inside an enum so I can change static values inside that class by just referencing building type?

eg

BuildingType.MINER.getReferencedClass.setCookiesPerSecond(4);

Thanks all,
Jaco :)

You can do this using reflection, however it would be much better to have an instance for that class which implements an interface that all the other implementations implement even if there is only even one instance of that interface eg

enum StaticData implement IData {
    INSTANCE; // only one instance

    /* put your non static fields here */

    /* put your methods for IData here */
}

public enum BuildingType {
    MINER("Miner", "A basic slave.", StaticData.INSTANCE), FARM("Farm", "Old Macdonald's Crib."), BAKERY("Bakery", "Best Cookies in Town!"), FACTORY("Factory", "Highest Quality Memes in town!"), QUARRY("Quarry", "Let's get drilling!");

// later
BuildingType.MINER.getIData().setCookiesPerSecond(4);

Yes, you can reference classes by their name followed by .class idiom. To show it in you example:

public enum BuildingType {
    MINER("Miner", "A basic slave.", Miner.class), FARM("Farm", "Old Macdonald's Crib.", Farm.class),
    BAKERY("Bakery", "Best Cookies in Town!", Bakery.class),
    FACTORY("Factory", "Highest Quality Memes in town!", Factory.class),
    QUARRY("Quarry", "Let's get drilling!", Quarry.class);

    private String name;
    private String description;
    private Class<?> clazz;

    BuildingType(String name, String description, Class<?> clazz) {
        this.name = name;
        this.description = description;
        this.clazz = clazz;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public Class<?> getReferencedClass() {
        return clazz;
    }

    public static class Miner {}

    public static class Farm {}

    public static class Bakery {}

    public static class Factory {}

    public static class Quarry {}
}

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