zoukankan      html  css  js  c++  java
  • java 嵌套接口

    接口可以嵌套在其它类或接口中,可以拥有public和"包访问权限"两种可见性

    作为一种新添加的方式,接口也可以实现为private

    当实现某个接口时,并不需要实现嵌套在其内的任何接口,而去private接口不能在定义它的类之外被实现

    //: interfaces/nesting/NestingInterfaces.java
    package object;
    
    class A {
      interface B {
        void f();
      }
      public class BImp implements B {
        public void f() {}
      }
      private class BImp2 implements B {
        public void f() {}
      }
      public interface C {
        void f();
      }
      class CImp implements C {
        public void f() {}
      }    
      private class CImp2 implements C {
        public void f() {}
      }
      private interface D {
        void f();
      }
      private class DImp implements D {
        public void f() {}
      }
      public class DImp2 implements D {
        public void f() {}
      }
      public D getD() { return new DImp2(); }
      private D dRef;
      public void receiveD(D d) {
        dRef = d;
        dRef.f();
      }
    }    
    
    interface E {
      interface G {
        void f();
      }
      // Redundant "public":
      public interface H {
        void f();
      }
      void g();
      // Cannot be private within an interface:
      //! private interface I {}
    }    
    
    public class NestingInterfaces {
      public class BImp implements A.B {
        public void f() {}
      }
      class CImp implements A.C {
        public void f() {}
      }
      // Cannot implement a private interface except
      // within that interface's defining class:
      //! class DImp implements A.D {
      //!  public void f() {}
      //! }
      class EImp implements E {
        public void g() {}
      }
      class EGImp implements E.G {
        public void f() {}
      }
      class EImp2 implements E {
        public void g() {}
        class EG implements E.G {
          public void f() {}
        }
      }    
      public static void main(String[] args) {
        A a = new A();
        // Can't access A.D:
        //! A.D ad = a.getD();
        // Doesn't return anything but A.D:
        //! A.DImp2 di2 = a.getD();
        // Cannot access a member of the interface:
        //! a.getD().f();
        // Only another A can do anything with getD():
        A a2 = new A();
        a2.receiveD(a.getD());//要使用D 只能把它交给有权使用它的对象
      }
    } ///:~
  • 相关阅读:
    Linux指令面试题01-进程查看与终止
    微信网页授权
    腾讯视频怎么转成mp4模式 软件 工具 方法 最新【已解决】
    表操作,多对一、多对多、一对一
    初识数据库,基础sql语句
    IO多路复用
    协程:gevent
    线程:threading
    进程:multiprocessing
    利用socket与ssl模块读取网页内容
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/10214150.html
Copyright © 2011-2022 走看看