Java 接口内的类
我试图开发一个接口,该接口将包含静态类Java 接口内的类,java,Java,我试图开发一个接口,该接口将包含静态类 class C1 { static interface I // A static interface or class can contain static members.Static members can be //accessed without instantiating the particular class { static class C2 { } } pub
class C1 {
static interface I // A static interface or class can contain static members.Static members can be
//accessed without instantiating the particular class
{
static class C2 {
}
}
public static void main(String a[]) {
C1.I.C2 ob1 = new C1.I.C2();
System.out.println("object created");
}
}
但我的问题是,接口可以包含非静态的类,如果是,那么它们的对象将如何创建,请给出建议。谢谢
接口可以包含类吗
对。例如,在
interface Widget {
static class Factory {
static Widget create() { return new Widget() {}; }
}
}
内部类可以按如下方式访问:
Widget w = Widget.Factory.create();
因此,要引用内部类,可以先使用接口名,然后使用点,再使用内部类名
import my.pkg.MyInterface;
...
MyInterface.InnerClass ic = new MyInterface.InnerClass();
接口可以包含类吗
对。例如,在
interface Widget {
static class Factory {
static Widget create() { return new Widget() {}; }
}
}
内部类可以按如下方式访问:
Widget w = Widget.Factory.create();
因此,要引用内部类,可以先使用接口名,然后使用点,再使用内部类名
import my.pkg.MyInterface;
...
MyInterface.InnerClass ic = new MyInterface.InnerClass();
请检查此plz:@assylias..我仍在对此进行研发。。。!!可能重复检查此plz:@assylias..我仍在对此进行研发。。。!!可能重复谢谢。这就是我要找的。谢谢。这就是我要找的。