代码之家  ›  专栏  ›  技术社区  ›  Vlad Gudim nuriaion

在Java接口中定义类的能力的实用方面?

  •  12
  • Vlad Gudim nuriaion  · 技术社区  · 17 年前

    interface IFoo
    {
        class Bar
        {
            void foobar ()
            {
                System.out.println("foobaring...");
            }
        }
    }
    
    11 回复  |  直到 17 年前
        1
  •  18
  •   Vlad Gudim nuriaion    17 年前

    除了Eric P链接的用法之外,我还可以想到另一种用法:定义接口的默认/无操作实现。

    ./亚历克斯

    interface IEmployee
    {
    
        void workHard ();  
        void procrastinate ();
    
        class DefaultEmployee implements IEmployee 
        {
            void workHard () { procrastinate(); };
            void procrastinate () {};
        }
    
    }
    

    另一个示例实现 Null Object Pattern :

    interface IFoo
    {
        void doFoo();
        IFoo NULL_FOO = new NullFoo();
    
        final class NullFoo implements IFoo
        {
            public void doFoo () {};
            private NullFoo ()  {};
        }
    }
    
    
    ...
    IFoo foo = IFoo.NULL_FOO;
    ...
    bar.addFooListener (foo);
    ...
    
        2
  •  8
  •   Eric Petroelje    17 年前

    我认为 this page 很好地解释了一个例子。您将使用它将特定类型紧密绑定到接口。

    interface employee{
        class Role{
              public String rolename;
              public int roleId;
         }
        Role getRole();
        // other methods
    }
    

    在上面的界面中,您将角色类型强绑定到员工界面(employee.Role)。

        3
  •  6
  •   Adam Crume    17 年前

    一种用途(无论好坏)是作为Java不支持接口中的静态方法这一事实的解决方法。

    interface Foo {
        int[] getData();
    
        class _ {
            static int sum(Foo foo) {
                int sum = 0;
                for(int i: foo.getData()) {
                    sum += i;
                }
                return sum;
            }
        }
    }
    

    int sum = Foo._.sum(myFoo);
    
        4
  •  5
  •   cletus    17 年前

    我可以毫不犹豫地说,我从来没有这样做过。我想不出你为什么也这么做。类嵌套在类中?当然,有很多理由这样做。在这些情况下,我倾向于将这些内部类视为实现细节。显然,接口没有实现细节。

        5
  •  3
  •   Alex Miller    17 年前

    这个成语被大量使用的一个地方是 XMLBeans 该项目的目的是采用XML模式并生成一组Java类,您可以双向使用这些类来处理与模式对应的XML文档。因此,它允许您将XML解析为xmlbean,或创建xmlbean并输出到XML。

    一般来说,大多数xml模式类型都映射到Java接口。该接口内部有一个Factory,用于在默认实现中生成该接口的实例:

    public interface Foo extends XmlObject {
      public boolean getBar();
      public boolean isSetBar();
      public void setBar(boolean bar);
    
      public static final SchemaType type = ...
    
      public static final class Factory {
        public static Foo newInstance() {
          return (Foo)XmlBeans.getContextTypeLoader().newInstance(Foo.type, null);
        }
    
        // other factory and parsing methods
      }
    } 
    

    我把它用于有意义的类(特别是那些我对接口/impls有很大控制权的类),发现它相当干净。

        6
  •  2
  •   Dan Dyer    17 年前

        7
  •  2
  •   user57697    17 年前

    public interface LoginService extends RemoteService {
    
        /**
         * Utility/Convenience class.
         * Use LoginService.App.getInstance() to access static instance of LoginServiceAsync
         */
        class App {
    
            public static synchronized LoginServiceAsync getInstance() {
                ...
            }
        }
    }
    
        8
  •  2
  •   Johannes B    17 年前

    public interface Printable {
        void print();
    
        public static class Caller {
            public static void print(Object mightBePrintable) {
                if (mightBePrintable instanceof Printable) {
                    ((Printable) mightBePrintable).print();
                }
            }
        }
    }
    

    现在,与其这样做:

    void genericPrintMethod(Object obj) {
        if (obj instanceof Printable) {
            ((Printable) obj).print();
        }
    }
    

    你可以写:

    void genericPrintMethod(Object obj) {
       Printable.Caller.print(obj);
    }
    
        9
  •  1
  •   Stefan Sveidqvist    17 年前

        10
  •  1
  •   Tom Hawtin - tackline    17 年前

    每当创建非私有嵌套类似乎是个好主意时,我都会敦促大家谨慎行事。几乎可以肯定,你最好直接去上课外课。但是,如果你要创建一个公共嵌套类,将其放在接口中似乎并不比放在类中更奇怪。外部类的抽象性不一定与嵌套类的抽象度有关。

        11
  •  1
  •   Peter Lawrey    17 年前

    推荐文章