是否应该使用或不使用公共访问修饰符来声明 Java 接口中的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/161633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:56:09  来源:igfitidea点击:

Should methods in a Java interface be declared with or without a public access modifier?

javainterfacecoding-stylepublic-method

提问by Benno Richters

Should methods in a Java interface be declared with or without the publicaccess modifier?

是否应该使用或不使用public访问修饰符来声明 Java 接口中的方法?

Technically it doesn't matter, of course. A class method that implements an interfaceis always public. But what is a better convention?

当然,从技术上讲,这无关紧要。实现 an 的类方法interface始终是public。但什么是更好的约定?

Java itself is not consistent in this. See for instance Collectionvs. Comparable, or Futurevs. ScriptEngine.

Java 本身在这方面并不一致。参见例如Collectionvs.ComparableFuturevs. ScriptEngine

采纳答案by Jon Skeet

The JLSmakes this clear:

JLS了这一点:

It is permitted, but discouraged as a matter of style, to redundantly specify the publicand/or abstractmodifier for a method declared in an interface.

允许但不鼓励为接口中声明的方法冗余指定public和/或abstract修饰符。

回答by JeeBee

I always write what I would use if there was no interface and I was writing a direct implementation, i.e., I would use public.

如果没有接口并且我正在编写直接实现,我总是写我会使用的东西,即,我会使用public.

回答by Rasmus Faber

The public modifier should be omitted in Java interfaces (in my opinion).

在 Java 接口中应该省略 public 修饰符(在我看来)。

Since it does not add any extra information, it just draws attention away from the important stuff.

由于它没有添加任何额外的信息,它只是将注意力从重要的东西上移开。

Most style-guides will recommend that you leave it out, but of course, the most important thing is to be consistent across your codebase, and especially for each interface. The following example could easily confuse someone, who is not 100% fluent in Java:

大多数风格指南会建议您将其排除在外,但当然,最重要的是在您的代码库中保持一致,尤其是对于每个界面。下面的例子很容易让那些对 Java 不是 100% 流利的人感到困惑:

public interface Foo{
  public void MakeFoo();
  void PerformBar();
}

回答by PhiLho

I would avoid to put modifiers that are applied by default. As pointed out, it can lead to inconsistency and confusion.

我会避免放置默认应用的修饰符。正如所指出的,它会导致不一致和混乱。

The worst I saw is an interface with methods declared abstract...

我看到的最糟糕的是一个带有声明方法的接口abstract......

回答by serg10

It's totally subjective. I omit the redundant publicmodifier as it seems like clutter. As mentioned by others - consistency is the key to this decision.

这完全是主观的。我省略了多余的public修饰符,因为它看起来很杂乱。正如其他人所提到的 - 一致性是这个决定的关键。

It's interesting to note that the C# language designers decided to enforce this. Declaring an interface method as public in C# is actually a compile error.Consistency is probably not important across languages though, so I guess this is not really directly relevant to Java.

有趣的是,C# 语言设计者决定强制执行此操作。 在 C# 中将接口方法声明为 public 实际上是一个编译错误。不过,跨语言的一致性可能并不重要,所以我想这与 Java 没有直接关系。

回答by cretzel

I used declare methods with the publicmodifier, because it makes the code more readable, especially with syntax highlighting. In our latest project though, we used Checkstyle which shows a warning with the default configuration for publicmodifiers on interface methods, so I switched to ommitting them.

我使用了带有public修饰符的声明方法,因为它使代码更具可读性,尤其是语法高亮显示。不过,在我们最新的项目中,我们使用了 Checkstyle,它显示了一个带有public接口方法修饰符默认配置的警告,所以我转而忽略它们。

So I'm not really sure what's best, but one thing I really don't like is using public abstracton interface methods. Eclipse does this sometimes when refactoring with "Extract Interface".

所以我不确定什么是最好的,但我真正不喜欢的一件事是public abstract在接口方法上使用。Eclipse 有时会在使用“提取接口”重构时执行此操作。

回答by Pradeep Sharma

I prefer skipping it, I read somewhere that interfaces are by default, publicand abstract.

我更喜欢跳过它,我在某处读到接口是默认的,public并且abstract.

To my surprise the book - Head First Design Patterns, is using publicwith interface declaration and interface methods... that made me rethink once again and I landed up on this post.

令我惊讶的是,这本书 - Head First Design Patterns使用public了接口声明和接口方法......这让我再次重新思考,我登上了这篇文章。

Anyways, I think redundant information should be ignored.

无论如何,我认为应该忽略冗余信息。

回答by Tim Boudreau

People will learn your interface from code completion in their IDE or in Javadoc, not from reading the source. So there's no point in putting "public" in the source - nobody's reading the source.

人们将通过 IDE 或 Javadoc 中的代码完成来了解您的界面,而不是通过阅读源代码。所以将“公开”放在源代码中是没有意义的——没有人在阅读源代码。

回答by Iuliana Cosmina

The reason for methods in interfaces being by default public and abstract seems quite logical and obvious to me.

接口中的方法默认为 public 和 abstract 的原因对我来说似乎非常合乎逻辑和显而易见。

A method in an interface it is by default abstract to force the implementing class to provide an implementation and is public by default so the implementing class has access to do so.

接口中的方法默认情况下是抽象的,以强制实现类提供实现,默认情况下是公共的,因此实现类可以访问。

Adding those modifiers in your code is redundant and useless and can only lead to the conclusion that you lack knowledge and/or understanding of Java fundamentals.

在您的代码中添加这些修饰符是多余且无用的,只会导致您缺乏对 Java 基础知识和/或理解的结论。

回答by Leo The Four

Despite the fact that this question has been asked long time ago but I feel a comprehensive description would clarify why there is no need to use public abstract before methods and public static final before constants of an interface.

尽管这个问题很久以前就被问过了,但我觉得一个全面的描述会澄清为什么不需要在方法之前使用 public abstract 和在接口的常量之前使用 public static final。

First of all Interfaces are used to specify common methods for a set of unrelated classes for which every class will have a unique implementation. Therefore it is not possible to specify the access modifier as private since it cannot be accessed by other classes to be overridden.

首先,接口用于为一组不相关的类指定通用方法,每个类都有一个唯一的实现。因此,不可能将访问修饰符指定为私有,因为其他类无法对其进行覆盖。

Second, Although one can initiate objects of an interface type but an interface is realized by the classes which implement it and not inherited. And since an interface might be implemented (realized) by different unrelated classes which are not in the same package therefore protected access modifier is not valid as well. So for the access modifier we are only left with public choice.

其次,虽然可以初始化接口类型的对象,但接口是由实现它的类实现的,而不是继承的。并且由于接口可能由不在同一包中的不同不相关类实现(实现),因此受保护的访问修饰符也无效。所以对于访问修饰符,我们只剩下公共选择。

Third, an interface does not have any data implementation including the instance variables and methods. If there is logical reason to insert implemented methods or instance variables in an interface then it must be a superclass in an inheritance hierarchy and not an interface. Considering this fact, since no method can be implemented in an interface therefore all the methods in interface must be abstract.

第三,接口没有任何数据实现,包括实例变量和方法。如果有逻辑原因在接口中插入实现的方法或实例变量,那么它必须是继承层次结构中的超类,而不是接口。考虑到这一事实,由于接口中不能实现任何方法,因此接口中的所有方法都必须是抽象的。

Fourth, Interface can only include constant as its data members which means they must be final and of course final constants are declared as static to keep only one instance of them. Therefore static final also is a must for interface constants.

第四,接口只能包含常量作为其数据成员,这意味着它们必须是最终的,当然最终常量被声明为静态以仅保留它们的一个实例。因此 static final 也是接口常量的必备条件。

So in conclusion although using public abstract before methods and public static final before constants of an interface is valid but since there is no other options it is considered redundant and not used.

因此,总而言之,尽管在接口的方法之前使用公共抽象和在接口常量之前使用公共静态最终是有效的,但由于没有其他选项,它被认为是多余的并且没有使用。