Yat*_*ney 9 java static interface
成员接口只能在顶级类或接口内或静态上下文中定义.
案例A:顶级类中的接口完美运行
package multiplei.interfaces.test;
public class InterfaceBetweenClass {
interface Foo {
void show();
}
class InnerClass implements Foo{
public void show(){
System.out.println("Inner Class implements Foo");
}
}
public static void main(String[] args) {
new InterfaceBetweenClass().new InnerClass().show();
}
}
Run Code Online (Sandbox Code Playgroud)
案例B:界面内的界面运行良好.
public interface Creatable {
interface Foo{
void show();
}}
Run Code Online (Sandbox Code Playgroud)
案例C:我知道为什么有人会在静态上下文中定义接口听起来很愚蠢.但是当我尝试在静态上下文中定义接口时,它会给我相同的错误消息.
package multiplei.interfaces.test;
public class InterfaceBetweenClass {
public static void main(String[] args) {
interface Foo { //Line 5
void show();
}
}
}}
Run Code Online (Sandbox Code Playgroud)
但第5行给出了以下错误消息"The member interface Foo can only be defined inside a top-level class or interface or in a static context."请帮我解决这个问题如果可以在静态上下文中定义接口那么如何?
您不能在方法内定义接口。
我认为错误消息所指的场景是在内部类中定义一个接口(可以这样做,但前提是它是一个static内部类):
class A{
static class X{
interface Y{}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
568 次 |
| 最近记录: |