在Java中,什么时候应该在接口中使用私有实例方法?

从 Java 9 开始,接口中的方法可以是私有的。私有方法可以是静态方法或实例方法。由于私有方法只能在接口本身的方法中使用,因此它们的使用仅限于作为接口其他方法的辅助方法。

Cay S. Horstmann,核心 Java 第 I 卷 - 基础知识

我知道我们可以将通用功能放在私有方法中,而不是使其可供公众访问。但我们在这里可以有两种私有方法:

  1. private

  2. private static

使用private static方法是可以理解的,但是什么时候应该使用private方法呢?我们在这里不处理实例,因为这是一个接口,那么为什么private允许创建方法呢?我们不需要的只是private static方法吗?


慕婉清6462132
浏览 87回答 2
2回答

幕布斯6054654

接口用于定义对象的行为。这意味着所有接口的方法都被公开。当使用默认方法时,我们可以提供已定义方法的标准实现,从而提供跨类边界的代码重用。在某些情况下,功能是必需的(也许只是为了在不同的默认方法中重用代码),但不应公开,因为它会污染类/对象的名称空间。这就是私有默认方法派上用场的地方。私有默认方法的示例可以是工厂、验证或默认状态处理。package com.company;import java.util.List;import java.util.function.Predicate;import java.util.stream.Collectors;public class Main {&nbsp; public static void main(final String[] args) {&nbsp; &nbsp; var messages =&nbsp; &nbsp; &nbsp; &nbsp; List.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageQueue.newSubject("Message 1"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageQueue.newTopic("Message 2"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageQueue.newTopic("Message 3"));&nbsp; &nbsp; final MessageQueueAdapter1 queue1 = () -> messages;&nbsp; &nbsp; inspectQueue(queue1);&nbsp; &nbsp; final MessageQueueAdapter2 queue2 = () -> messages;&nbsp; &nbsp; inspectQueue(queue2);&nbsp; }&nbsp; private static void inspectQueue(final MessageQueue queue) {&nbsp; &nbsp; final List<Message> messagesWithSubject = queue.getMessagesWithSubject();&nbsp; &nbsp; assert messagesWithSubject.size() == 1 : "expected one message with 'Subject'";&nbsp; &nbsp; final List<Message> messagesWithTopic = queue.getMessagesWithTopic();&nbsp; &nbsp; assert messagesWithTopic.size() == 2 : "expected two message with 'Topic'";&nbsp; &nbsp; assert !queue.getMessages().isEmpty() && 3 == queue.getMessages().size()&nbsp; &nbsp; &nbsp; &nbsp; : "expected three messages in total";&nbsp; }&nbsp; @FunctionalInterface&nbsp; interface Message {&nbsp; &nbsp; private static boolean isPrefixedBy(final String message, final String prefix) {&nbsp; &nbsp; &nbsp; return message != null && !message.isEmpty() && message.startsWith(prefix);&nbsp; &nbsp; }&nbsp; &nbsp; default boolean hasSubject() {&nbsp; &nbsp; &nbsp; return isPrefixedBy(this.getMessage(), MessageQueue.PREFIX_SUBJECT);&nbsp; &nbsp; }&nbsp; &nbsp; default boolean hasTopic() {&nbsp; &nbsp; &nbsp; return isPrefixedBy(this.getMessage(), MessageQueue.PREFIX_TOPIC);&nbsp; &nbsp; }&nbsp; &nbsp; String getMessage();&nbsp; }&nbsp; interface MessageQueue {&nbsp; &nbsp; String PREFIX_SUBJECT = "Subject: ";&nbsp; &nbsp; String PREFIX_TOPIC = "Topic: ";&nbsp; &nbsp; private static Message newMessage(final String message) {&nbsp; &nbsp; &nbsp; return () -> message;&nbsp; &nbsp; }&nbsp; &nbsp; static Message newSubject(final String message) {&nbsp; &nbsp; &nbsp; return newMessage(PREFIX_SUBJECT + message);&nbsp; &nbsp; }&nbsp; &nbsp; static Message newTopic(final String message) {&nbsp; &nbsp; &nbsp; return newMessage(PREFIX_TOPIC + message);&nbsp; &nbsp; }&nbsp; &nbsp; List<Message> getMessages();&nbsp; &nbsp; List<Message> getMessagesWithSubject();&nbsp; &nbsp; List<Message> getMessagesWithTopic();&nbsp; }&nbsp; @FunctionalInterface&nbsp; interface MessageQueueAdapter1 extends MessageQueue {&nbsp; &nbsp; private static List<Message> filterBy(&nbsp; &nbsp; &nbsp; &nbsp; final List<Message> messages, final Predicate<Message> predicate) {&nbsp; &nbsp; &nbsp; return messages.stream().filter(predicate).collect(Collectors.toList());&nbsp; &nbsp; }&nbsp; &nbsp; /** {@inheritDoc} */&nbsp; &nbsp; @Override&nbsp; &nbsp; default List<Message> getMessagesWithSubject() {&nbsp; &nbsp; &nbsp; return filterBy(this.getMessages(), Message::hasSubject);&nbsp; &nbsp; }&nbsp; &nbsp; /** {@inheritDoc} */&nbsp; &nbsp; @Override&nbsp; &nbsp; default List<Message> getMessagesWithTopic() {&nbsp; &nbsp; &nbsp; return filterBy(this.getMessages(), Message::hasTopic);&nbsp; &nbsp; }&nbsp; }&nbsp; @FunctionalInterface&nbsp; interface MessageQueueAdapter2 extends MessageQueue {&nbsp; &nbsp; private List<Message> filterBy(final Predicate<Message> predicate) {&nbsp; &nbsp; &nbsp; return this.getMessages().stream().filter(predicate).collect(Collectors.toList());&nbsp; &nbsp; }&nbsp; &nbsp; /** {@inheritDoc} */&nbsp; &nbsp; @Override&nbsp; &nbsp; default List<Message> getMessagesWithSubject() {&nbsp; &nbsp; &nbsp; return filterBy(Message::hasSubject);&nbsp; &nbsp; }&nbsp; &nbsp; /** {@inheritDoc} */&nbsp; &nbsp; @Override&nbsp; &nbsp; default List<Message> getMessagesWithTopic() {&nbsp; &nbsp; &nbsp; return filterBy(Message::hasTopic);&nbsp; &nbsp; }&nbsp; }}

慕桂英4014372

好的,再次尝试实际回答OP的问题。当需要从私有方法调用接口上的另一个非静态方法时,私有方法不能是静态的。例如,如果下面的私有方法是静态的,就会出现编译错误:public interface InterfaceWithMethods {&nbsp; &nbsp; public default void doSomething() {&nbsp; &nbsp; &nbsp; &nbsp; doSomethingCommon();&nbsp; &nbsp; }&nbsp; &nbsp; public default void doSomethingElse() {&nbsp; &nbsp; &nbsp; &nbsp; doSomethingCommon();&nbsp; &nbsp; }&nbsp; &nbsp; public void actuallyDoSomething();&nbsp; &nbsp; private void doSomethingCommon() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Do something first.");&nbsp; &nbsp; &nbsp; &nbsp; actuallyDoSomething();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java