[英]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
可以是类定义的成员,并且可以在其中声明为private
或protected
。
public class Test {
private interface Sortable {
}
protected interface Searchable {
}
}
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. 接口上的public
, private
和protected
访问修饰符的含义与它们在类上的含义相同。 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.