我有以下课程。我试图让它成为getConfig()将返回指定的类型,但是,它只返回BaseConfig。假设我有扩展BaseConfig的MyConfig,我希望该方法返回MyConfig而不是BaseConfig。非常感谢任何帮助,因为我似乎无法找到适合我的合适解决方案。
public class LoadedMap<T extends BaseConfig> implements GameMap {
private File world;
private Class<T> configClass;
private T config;
public LoadedMap(File world, Class<T> configClass) {
this.world = world;
this.configClass = configClass;
this.loadConfig();
}
@Override
public File getWorld() {
return this.world;
}
@Override
public String getMapName() {
return this.config.getMapName();
}
@Override
public String getMapAuthor() {
return this.config.getMapAuthor();
}
public <T extends BaseConfig> T getConfig() {
return configClass.cast(config);
}
private void loadConfig() {
this.config = JsonConfig.load(new File(world + File.separator + "map.json"), configClass);
}
}
构成我正在研究的系统的一些其他类:
public class MapManager<T extends BaseConfig> {
private Class<T> configClass;
private List<LoadedMap<T>> maps = new ArrayList<>();
private LoadedMap<T> current = null;
public MapManager(Class<T> configClass) {
this.configClass = configClass;
}
public CycleCompleteEvent cycle() throws MapNotFoundException{
load();
if (maps.size() == 0) {
throw new MapNotFoundException();
}
LoadedMap previous = current;
if (maps.contains(current) && maps.indexOf(current) != maps.size() - 1) {
current = maps.get(maps.indexOf(current) + 1);
} else {
current = maps.get(0);
}
MapSelectedEvent mapSelectedEvent = new MapSelectedEvent(current);
Bukkit.getPluginManager().callEvent(mapSelectedEvent);
if (mapSelectedEvent.getMap() != null) {
current = mapSelectedEvent.getMap();
}
init();
if (current != null) {
clean(previous);
}
CycleCompleteEvent cycleCompleteEvent = new CycleCompleteEvent(current);
Bukkit.getPluginManager().callEvent(cycleCompleteEvent);
return cycleCompleteEvent;
}
private void init() {
prepareWorld();
}
private void init(LoadedMap<T> map) {
if (current != null) {
clean(current);
}
current = map;
prepareWorld();
}
private void prepareWorld() {
WorldCreator creator = new WorldCreator(current.getWorld().getName());
Bukkit.getPluginManager().callEvent(new PreMapLoadEvent(current, creator));
World world = Bukkit.createWorld(creator);
world.setAutoSave(false);
Bukkit.getPluginManager().callEvent(new PostMapLoadEvent(current, world));
}
private void clean(LoadedMap map) {
if (Bukkit.getWorlds().contains(map.getWorld().getName())) {
Bukkit.unloadWorld(map.getWorld().getName(), false);
}
}
public void load() {
File worldContainer = Bukkit.getWorldContainer();
for (File file : worldContainer.listFiles()) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
if (f.getName().equals("map.json")) {
for (LoadedMap loadedMap : maps) {
if (loadedMap.getWorld() == file) {
continue;
}
}
maps.add(new LoadedMap<>(file, configClass));
}
}
}
}
}
public Class<T> getConfigClass() {
return configClass;
}
public List<LoadedMap<T>> getMaps() {
load();
return maps;
}
public LoadedMap<T> getCurrent() {
return current;
}
}
另一个:
public abstract class Game<T extends BaseConfig> extends BukkitRunnable {
private Plugin plugin;
private boolean cancelled = false;
private MapManager<T> mapManager;
public Game(Plugin plugin, Class<T> configClass) {
this.plugin = plugin;
this.mapManager = new MapManager(configClass);
this.runTaskTimer(plugin, 1, 1);
}
public abstract void run();
@Override
public void cancel() {
if (this.cancelled) {
return;
}
this.cancelled = true;
Bukkit.getScheduler().cancelTask(getTaskId());
}
public Plugin getPlugin() {
return plugin;
}
public boolean isCancelled() {
return cancelled;
}
public MapManager getMapManager() {
return mapManager;
}
}
答案 0 :(得分:3)
您正在使您的方法具有通用性,但它不应该是:该类已经是通用的。它应该简单地声明为
public T getConfig() {
return configClass.cast(config);
}
这样,方法返回的类型T是在类级别定义的类型T.你这样做的方式,T是另一种泛型类型,特定于碰巧与类泛型类型同名的方法,但不是相同的。即它相当于
public <U extends BaseConfig> U getConfig() {
return configClass.cast(config);
}