Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 接口内的类_Java - Fatal编程技术网

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..我仍在对此进行研发。。。!!可能重复谢谢。这就是我要找的。谢谢。这就是我要找的。