[英]Private interface methods are supported
Private interface methods are supported by Java 9. Java 9支持专用接口方法。
This support allows non-abstract methods of an interface
to share code between them. 此支持允许
interface
非抽象方法在它们之间共享代码。 Private methods can be static or instance. 私有方法可以是静态的或实例的。
Can private methods of an interface be abstract
or default
? 接口的私有方法可以是
abstract
还是default
?
May I ask for an example where " private static
interface methods" are useful in terms of code? 我可以问一个例子,“
private static
接口方法”在代码方面是否有用?
No , the private methods in the interfaces are supposedly designed for clubbing in a piece of code that is internal to the interface
implementation. 不 ,接口中的私有方法应该被设计用于在
interface
实现内部的一段代码中进行分组。 Since these pertain to the implementation(consist of a body) and not the declaration it can neither be default
and nor abstract
when defined. 由于这些属于实现(由主体组成)而不是声明,因此在定义时既不能是
default
也不是abstract
。
A private
method is a static
method or a non-default instance method that's declared with the private
keyword. private
方法是static
方法或使用private
关键字声明的非默认实例方法。 You cannot declare a default
method to also be private
because default
methods are intended to be callable from the classes that implement their declaring interfaces. 您不能将
default
方法声明为private
因为default
方法可以从实现其声明接口的类中调用。
The private static
methods are useful in abstracting a common piece of code from static
methods of an interface while defining its implementation. private static
方法在定义其实现时从接口的static
方法中抽象出一段共同的代码是很有用的。
Example of a private static method in an interface could be as follows. 接口中的私有静态方法的示例可以如下。 Consider an object,
Question.java
on StackOverflow defined as: 考虑一个对象,StackOverflow上的
Question.java
定义为:
class Question {
int votes;
long created;
}
and an interface that proposes the sort by functionality as seen in the listed questions on StackOverflowTag
: 和一个提出按功能排序的接口,如
StackOverflowTag
列出的问题所示:
public interface StackOverflowTag {
static List<Question> sortByNewest(List<Question> questions) {
return sortBy("NEWEST", questions);
}
static List<Question> sortByVotes(List<Question> questions) {
return sortBy("VOTE", questions);
}
//... other sortBy methods
private static List<Question> sortBy(String sortByType, List<Question> questions) {
if (sortByType.equals("VOTE")) {
// sort by votes
}
if (sortByType.equals("NEWEST")) {
// sort using the created timestamp
}
return questions;
}
}
Here the private static
method sortBy
of the interface internally implements the sorting based on the sortOrderType
sharing the implementation with two public static methods of the interface which can be further consumed by a StackOverflowTagConsumer
can simply access these interface static methods as : 这里接口的
private static
方法sortBy
在内部实现基于sortOrderType
的排序,该接口通过接口的两个公共静态方法共享实现, StackOverflowTagConsumer
可以进一步使用这些方法,只需访问这些接口静态方法:
public class StackOverFlowTagConsumer {
public static void main(String[] args) {
List<Question> currentQuestions = new ArrayList<>();
// if some action to sort by votes
displaySortedByVotes(currentQuestions);
// if another action to sort by newest
displaySortedByNewest(currentQuestions);
}
private static void displaySortedByVotes(List<Question> currentQuestions) {
System.out.println(StackOverflowTag.sortByVotes(currentQuestions));
}
private static void displaySortedByNewest(List<Question> currentQuestions) {
System.out.println(StackOverflowTag.sortByNewest(currentQuestions));
}
}
The default
keyword for interface methods exist, because for interface methods, abstract
is implicitly assumed if no other modifier contradicts it. 存在接口方法的
default
关键字,因为对于接口方法,如果没有其他修饰符与其相矛盾,则隐式假设为abstract
。 Before Java 8, this applied to all interface methods, which were always considered abstract
. 在Java 8之前,这适用于所有接口方法,这些方法总是被认为是
abstract
。
Since the presence of either, static
or private
, already implies that it cannot be abstract
(which applies to ordinary classes as well), there is no need to add a default
modifier and consequently, Java rules out this combination. 由于
static
或private
的存在已经暗示它不能是abstract
(也适用于普通类),因此不需要添加default
修饰符,因此Java排除了这种组合。 And there is no point in asking for this combination either, as default
merely implies that the method is not abstract
, technically, so adding it to a method which is already not abstract
wouldn't change anything. 要求这种组合也没有意义,因为
default
只是意味着该方法在技术上不是abstract
,所以将它添加到已经不是abstract
方法不会改变任何东西。
On the other hand, since the only methods needing a default
keyword for declaring that they are not abstract
, are public
instance methods, the default
keyword only applies to overridable methods, which conveniently matches the literal meaning of the word “default”. 另一方面,由于需要
default
关键字来声明它们不是abstract
的唯一方法是public
实例方法,因此default
关键字仅适用于可覆盖的方法,这些方法可以方便地匹配单词“default”的字面含义。
private
methods are useful to provide common operations for the public
non- abstract
methods of an interface when these common operations are not supposed to be called from the outside of the interface directly, much like private
methods in ordinary classes, further, they exist in Java 8 already on the byte code level, as default
and static
methods may contain lambda expressions which are compiled into synthetic private
methods, so there was no technical reason to deny that feature to the Java programming language. 当不应该直接从接口外部调用这些常见操作时,
private
方法对于为接口的public
非abstract
方法提供公共操作很有用,就像普通类中的private
方法一样,此外,它们存在于Java中8已经在字节码级别上,因为default
和static
方法可能包含lambda表达式,这些表达式被编译成合成private
方法,所以没有技术上的理由拒绝Java编程语言的这个特性。
No, these three combinations are mutually exclusive. 不,这三种组合是互斥的。 Interface methods cannot be at the same time:
接口方法不能同时:
default
means the opposite of abstract
) default
意味着与abstract
相反)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.