接口可以嵌套在类或其他接口中

    1. //: interfaces/nesting/NestingInterfaces.java
    2. package com.zx.chapter09.shipqi;
    3. class A {
    4. interface B {
    5. void f();
    6. }
    7. public class BImp implements B {
    8. public void f() {}
    9. }
    10. private class BImp2 implements B {
    11. public void f() {}
    12. }
    13. public interface C {
    14. void f();
    15. }
    16. class CImp implements C {
    17. public void f() {}
    18. }
    19. private class CImp2 implements C {
    20. public void f() {}
    21. }
    22. private interface D {
    23. void f();
    24. }
    25. private class DImp implements D {
    26. public void f() {}
    27. }
    28. public class DImp2 implements D {
    29. public void f() {}
    30. }
    31. public D getD() { return new DImp2(); }
    32. private D dRef;
    33. public void receiveD(D d) {
    34. dRef = d;
    35. dRef.f();
    36. }
    37. }
    38. interface E {
    39. interface G {
    40. void f();
    41. }
    42. public interface H {
    43. void f();
    44. }
    45. void g();
    46. }
    47. public class NestingInterfaces {
    48. public class BImp implements A.B {
    49. public void f() {
    50. }
    51. }
    52. class CImp implements A.C {
    53. public void f() {
    54. }
    55. }
    56. class EImp implements E {
    57. public void g() {
    58. }
    59. }
    60. class EGImp implements E.G {
    61. public void f() {
    62. }
    63. }
    64. class EImp2 implements E {
    65. public void g() {
    66. }
    67. class EG implements E.G {
    68. public void f() {
    69. }
    70. }
    71. }
    72. public static void main(String[] args) {
    73. A a = new A();
    74. A a2 = new A();
    75. a2.receiveD(a.getD());
    76. }
    77. }

    在类中嵌套的接口可以用private修饰,但是在接口中嵌套的接口则必须public 修饰