Ste*_*ose 1 java static nested interface abstract
我有嵌套类的Java问题.
我的第一类结构看起来像这样:
public class TopClass {
public void mainMethod() {
// uses the different "method" methods from
// NestedClass-implementing nested classes
}
private interface NestedClass {
public void method();
}
private class NestedClass1 {
public void method() {
}
}
private class NestedClass2 {
public void method(){
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在我希望这些method()
方法是静态的,因为它们应该是主要的.
如果不将它们放在静态类中,我就无法使它们成为静态,但这没有问题,我使这些类保持静态,它们应该是无论如何.
它现在看起来像这样:
public class TopClass {
public void mainMethod() {
// uses the different "method" methods from
// NestedClass-implementing nested classes
}
private static interface NestedClass {
public void method();
}
private static class NestedClass1 {
public static void method() {
}
}
private static class NestedClass2 {
public static void method(){
}
}
}
Run Code Online (Sandbox Code Playgroud)
但麻烦就开始了.静态方法无法从非静态接口方法正确继承,因为我This static method cannot hide the instance method from TopClass.NestedClass
在Eclipse中收到此消息.
当我使接口方法静态时,它给了我这个错误: Illegal modifier for the interface method method; only public & abstract are permitted
所以我想到了一个抽象类,并尝试了这个:
public class TopClass {
public void mainMethod() {
// uses the different "method" methods from
// NestedClass-implementing nested classes
}
private static abstract class NestedClass {
public static abstract void method();
}
private static class NestedClass1 {
public static void method() {
}
}
private static class NestedClass2 {
public static void method(){
}
}
}
Run Code Online (Sandbox Code Playgroud)
但同样,看似抽象的方法不能被声明为静态:The abstract method method in type NestedClass can only set a visibility modifier, one of public or protected
.
离开静态(在抽象类方法中),在NestedClass1&2中的方法方法上出错:This static method cannot hide the instance method from TopClass.NestedClass
.
是否有任何方法可以声明某种上层结构来覆盖静态方法?
编辑:问题我实际上试图解决它缺乏Java存储方法的引用的可能性.所以相反,我只有一种方法,每个人都有这些类,但是要将它们存储在List fe中,它们必须能够被上层结构"捕获".我得到了尝试匿名课程或枚举的提示,现在就试试吧.
归档时间: |
|
查看次数: |
1371 次 |
最近记录: |