简体   繁体   English

Java中的公共接口和私有接口有什么区别

[英]What is the difference between a public and private interface in Java

I know the difference between all the access modifiers in Java. 我知道Java中所有访问修饰符之间的区别。 However, someone asked me a very interesting question that I struggled to find the answer to: What is the difference between a private interface and a public interface in Java, in particular, how it is used as a class member ? 但是,有人问我一个非常有趣的问题,我很难找到答案:Java中的private接口和public接口之间有什么区别,特别是如何将其用作类成员 Any help would be greatly appreciated. 任何帮助将不胜感激。

I believe we all know the use of public interface , so I would mention the point of private/protected interface here. 我相信大家都知道public interface的用法,因此在这里我要提到private/protected interface

Interfaces can be members of class definitions and can be declared private or protected there. Interfaces可以是类定义的成员,并且可以在其中声明为privateprotected

public class Test {  

    private interface Sortable {  
    }  

   protected interface Searchable {  
    }  

} 

Example 1: -- Source 示例1:-源

public class PrivateInterface {  
     private interface InnerInterface {  
          void f();  
     }  

     private class InnerClass1 implements InnerInterface {  
           public void f() {   
               System.out.println("From InnerClass1");  
           }  
     }  

     private class InnerClass2 implements InnerInterface {  
           public void f() {   
               System.out.println("From InnerClass2");  
           }  
     }  

     public static void main(String[] args) {  
          PrivateInterface pi = new PrivateInterface();  
          pi.new InnerClass1().f();  
          pi.new InnerClass2().f();  
     }  
}   

/* Output: 
From InnerClass1 
From InnerClass2 
*/  

It's the interface itself that can be package-private, not the methods in it. 接口本身可以是包私有的,而不是其中的方法。 You can define an interface that can only be used (by name) within the package it's defined in, but its methods are public like all interface methods. 您可以定义一个只能在其定义的包中使用的接口(按名称),但其方法像所有接口方法一样是公共的。 If a class implements that interface, the methods it defines must be public. 如果类实现该接口,则它定义的方法必须是公共的。 The key thing here is that it's the interface type that isn't visible outside the package, not the methods. 这里的关键是接口类型在包外部不可见,而不是方法。

The public , private , and protected access modifiers on an interface mean the same thing that they mean on a class. 接口上的publicprivateprotected访问修饰符的含义与它们在类上的含义相同。 I typically see these modifiers used on an interface that is nested in a class. 我通常看到嵌套在类中的接口上使用的这些修饰符。 Something like this : 这样的东西:

//: interfaces/RandomWords.java  
// Implementing an interface to conform to a method.  
package interfaces;  

public class PrivateInterface {  
  private interface InnerInterface {  
        void f();  
  }  

  private class InnerClass1 implements InnerInterface {  
         public void f() {   
             System.out.println("From InnerClass1");  
         }  
  }  

  private class InnerClass2 implements InnerInterface {  
         public void f() {   
             System.out.println("From InnerClass2");  
         }  
  }  

  public static void main(String[] args) {  
        PrivateInterface pi = new PrivateInterface();  
        pi.new InnerClass1().f();  
        pi.new InnerClass2().f();  
  }  
}  

An interface declaration may include these access modifiers: 接口声明可以包括以下访问修饰符:

public protected private abstract static strictfp

public: If an interface type is declared public,then it can be accessed by any code. public:如果接口类型被声明为public,那么任何代码都可以访问它。

protected/private: The access modifiers protected and private pertain only to member interfaces within a directly enclosing class declaration. protected / private: protected和private访问修饰符仅与直接封闭的类声明中的成员接口有关。 A member interface is an interface whose declaration is directly enclosed in another class or interface declaration. member interface是其声明直接包含在另一个类或接口声明中的接口。

static: The access modifier static pertains only to member interfaces, not to top level interfaces. static:访问修饰符static仅与成员接口有关,与顶级接口无关。

abstract: Every interface is implicitly abstract . abstract:每个接口都是隐式abstract This modifier is obsolete and should not be used in new programs. 该修饰符已过时,不应在新程序中使用。

strictfp: The effect of the strictfp modifier is to make all float or double expressions within the interface declaration be explicitly FP-strict . strictfp: strictfp修饰符的作用是使接口声明中的所有float或double表达式都显式为FP-strict

Ref: Java Language and Virtual Machine Specifications 参考: Java语言和虚拟机规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java中public、protected、package-private和private有什么区别? - What is the difference between public, protected, package-private and private in Java? java中的interface和@interface有什么区别? - What's the difference between interface and @interface in java? public int和Java中int有什么区别? - What is the difference between public int and int in Java? 接口中的公共方法和抽象方法有什么区别? - What's the difference between public method and abstract method in Interface? 公共静态和私有静态变量之间的区别 - Difference between public static and private static variables java 9模块中没有访问说明符和public之间有什么区别? - What is the difference between no access specifier and public in java 9 module? 之间的区别是需要公共VS需要Java 9中的可传递 - What is difference between requires public VS requires transitive in Java 9 C ++ Concept和Java Interface之间有什么区别? - What's the difference between C++ Concept and Java Interface? Java8 中的谓词和函数接口有什么区别? - What is the difference between a Predicate and a Function Interface in Java8? 在接口和类java中声明常量之间有什么区别 - what is the difference between declaring constant in an interface and in class java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM