简体   繁体   English

支持专用接口方法

[英]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. 由于staticprivate的存在已经暗示它不能是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方法对于为接口的publicabstract方法提供公共操作很有用,就像普通类中的private方法一样,此外,它们存在于Java中8已经在字节码级别上,因为defaultstatic方法可能包含lambda表达式,这些表达式被编译成合成private方法,所以没有技术上的理由拒绝Java编程语言的这个特性。

No, these three combinations are mutually exclusive. 不,这三种组合是互斥的。 Interface methods cannot be at the same time: 接口方法不能同时:

  • Default and abstract (because default means the opposite of abstract ) 默认和抽象(因为default意味着与abstract相反)
  • Default and private (because you cannot override a private method) 默认和私有(因为您无法覆盖私有方法)
  • Abstract and private (because you cannot override a private method) 抽象和私有(因为你不能覆盖私有方法)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM