支持专用接口方法 [英] Private interface methods are supported

查看:129
本文介绍了支持专用接口方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 9支持私有接口方法。

此支持允许接口的非抽象方法在它们之间共享代码。私有方法可以是静态的或实例。

This support allows non-abstract methods of an interface to share code between them. Private methods can be static or instance.

接口的私有方法可以是 abstract 还是 default

Can private methods of an interface be abstract or default?

请问一个例子,其中 private static 接口方法在代码方面很有用吗?

May I ask for an example where "private static interface methods" are useful in terms of code?

推荐答案

,接口中的私有方法应该设计用于在接口实现内部的一段代码中进行分组。由于这些属于实现(由正文组成)而不是声明,因此它既不能默认也不能 abstract 定义时。

No, the private methods in the interfaces are supposedly designed for clubbing in a piece of code that is internal to the interface implementation. Since these pertain to the implementation(consist of a body) and not the declaration it can neither be default and nor abstract when defined.

私有方法是 static 方法或使用 private 关键字声明的非默认实例方法。您无法声明 默认 方法也是 private 因为默认方法可以从实现的类中调用他们的声明接口。

A private method is a static method or a non-default instance method that's declared with the private keyword. 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.

私有静态方法非常有用在定义其实现时,从接口的 static 方法中抽象出一段共同的代码。

The private static methods are useful in abstracting a common piece of code from static methods of an interface while defining its implementation.

接口中私有静态方法的示例如下。考虑一个对象, 问题。 StackOverflow上的java 定义为:

Example of a private static method in an interface could be as follows. Consider an object, Question.java on StackOverflow defined as:

class Question {
    int votes;
    long created;
}

以及建议按所列问题列出的功能排序的界面 StackOverflowTag

and an interface that proposes the sort by functionality as seen in the listed questions on 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;
    }
}

这里私有静态接口的方法 sortBy 在内部实现基于 sortOrderType 的排序,用两个共享实现接口的公共静态方法,可以通过 StackOverflowTagConsumer 可以简单地访问这些接口静态方法:

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 :

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.sortByNewest(currentQuestions));
    }

    private static void displaySortedByNewest(List<Question> currentQuestions) {
        System.out.println(StackOverflowTag.sortByNewest(currentQuestions));
    }
}

这篇关于支持专用接口方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆