以下是我在运行服务器时从netbeans IDE获取的错误。正如错误所说
无法编译的源代码 - Projects.ApplicationMenu不是抽象的 并且不会覆盖抽象方法setProject(Projects.Project) Projects.AbstractComponent
但没有错误。我检查了Menu超类,它确实覆盖了抽象方法。
Exception in thread "main" java.lang.ExceptionInInitializerError
at Projects.Project.initialize(Project.java:54)
at Projects.Project.<init>(Project.java:34)
at Server.FileIO.ProjectIO.ProjectIO.translate(ProjectIO.java:52)
at Server.FileIO.ProjectService.LoadProjects(ProjectService.java:55)
at Server.FileIO.ProjectService.<init>(ProjectService.java:42)
at Server.FileIO.FileService.<init>(FileService.java:35)
at Server.ServerInitialization.InitializeModelAndProjects(ServerInitialization.java:47)
at Server.Server.Initialize(Server.java:97)
at Server.Server.<init>(Server.java:46)
at Server.Server.main(Server.java:39)
Caused by: java.lang.RuntimeException: Uncompilable source code - Projects.ApplicationMenu is not abstract and does not override abstract method setProject(Projects.Project) in Projects.AbstractComponent
at Projects.UIComponent.<clinit>(UIComponent.java:15)
... 10 more
以下是ApplicationMenu类:
package Projects;
import Key.Key;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
/**
*
* @author Mark
*/
public class ApplicationMenu extends Menu{
MenuBar menubar;
ApplicationMenu parentmenu;
public ApplicationMenu(String name, MenuBar menubar, Key key) {
super(name, null, key, menubar.project);
this.menubar = menubar;
}
public ApplicationMenu(String name, ApplicationMenu parentmenu, Key key){
super(name, parentmenu, key, parentmenu.project);
}
public ApplicationMenu(JsonObject jsonApplicationMenu, String name, MenuBar menubar, Key key){
super(name, null, key, menubar.project);
this.menubar = menubar;
this.unPackComponent();
}
public ApplicationMenu addApplicationMenu(String name, Key key){
ApplicationMenu menu = new ApplicationMenu(name, this, key);
this.addChildMenu(menu);
return menu;
}
public ComponentMenu addComponentMenu(String name, Key key){
ComponentMenu menu = new ComponentMenu(name, this, key);
this.addChildMenu(menu);
return menu;
}
@Override
public JsonObject packComponent(){
JsonArrayBuilder arraybuilder = Json.createArrayBuilder();
this.childmenus.forEach((cmkey,menu)->{
arraybuilder.add(menu.packComponent());
});
packagedJsonArray = arraybuilder.build();
packagedJsonObject = Json.createObjectBuilder()
.add(Menu.childMenus(), packagedJsonArray)
.add( Menu.menuType(), ApplicationMenu.applicationMenu())
.add(AbstractComponent.name(), this.name)
.add(Key.key(),this.packKey()).build();
return packagedJsonObject;
}
public final static String applicationMenu(){return "ApplicationMenu";}
@Override
public final void unPackComponent() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
void translateFromHeFile() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
以下是ApplicationMenu的菜单超类:
package Projects;
import Key.Key;
import java.util.HashMap;
/**
*
* @author Mark
*/
public abstract class Menu extends UIComponent{
Menu parentMenu;
HashMap<Key, Menu> childmenus;
public Menu(String name, Menu parentMenu, Key key, Project project) {
super(name, parentMenu, key);
this.parentMenu = parentMenu;
childmenus = new HashMap<>();//
this.project = project;
}
public Menu addChildMenu(Menu menu){
childmenus.put(menu.key, menu);
return menu;
}
public String getName(){
return this.name;
}
@Override
protected void setProject(Project project){
this.project = project;
}
@Override
public Project getProject(){
return this.project;
}
public final static String childMenus(){return "ChildMenus";}
public final static String menuType(){return "MenuType";}
}
如您所见,我确实覆盖了该方法。我已经尝试多次重启IDE。我也曾多次尝试清理和构建所有依赖项目。我做的一件事是在项目之间移动一个包。我意识到由于某些依赖性,我无法将此包移动到公共项目。我把它移回后我开始收到这个错误。任何援助将不胜感激。
下面是AbstractComponent类:
package Projects;
import Key.Key;
import java.util.HashMap;
import javax.json.JsonArray;
import javax.json.JsonObject;
/**
*
* @author Mark
*/
public abstract class AbstractComponent implements Component {
String name;
Key key;
AbstractComponent parentComponent;
HashMap<Key, AbstractComponent> childComponents;
JsonObject packagedJsonObject;
JsonArray packagedJsonArray;
Project project;
public AbstractComponent( String name, AbstractComponent parentComponent, Key key){
this.name = name;
this.parentComponent = parentComponent;
this.key = key;
childComponents = new HashMap<>();
}
public AbstractComponent addChild(AbstractComponent projectelement){
childComponents.put(projectelement.key, projectelement);
return projectelement;
}
@Override
public Key getKey(){
return this.key;
}
public JsonObject packKey(){
return this.key.toJsonObject();
}
abstract JsonObject packComponent();
abstract void unPackComponent();
abstract void translateFromHeFile();
abstract protected void setProject(Project project);
public final static String name(){return "Name";}
}
答案 0 :(得分:1)
我猜测setProject方法起源于另一个包(可能是Component接口)中声明的接口或父类。如果是这样,那么您的抽象类将编译正常,但是一旦您尝试覆盖在不同包中声明的受保护方法,具体类将失败。
如果是这样,解决方案是将setProject公开。