Java 编译器允许我在 Interface 中编写 Class 定义。这有什么具体用途吗?
interface ClassInterface {
void returnSomething();
int x = 10;
class SomeClass {
private int y;
private void classDoingSomething() {
}
}
}
请解释。
最佳答案
用法与嵌套在另一个类中的类相同:它允许将类的范围限定为接口(interface)。你可以想象这样的事情:
public interface Switch {
public enum Status {
ON, OFF;
}
void doSwitch();
Status getStatus();
}
它避免定义一个名为 SwitchStatus
的顶级类(因为 Status
可能是一个过于通用的名称)。
关于java - 接口(interface)中的嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9098235/