Java 接口、抽象类和枚举命名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1006332/
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-10-29 14:46:37  来源:igfitidea点击:

Java Interface, AbstractClass and Enum naming convention

javanaming-conventions

提问by roundcrisis

We are having this discussion in our team about code conventions for Java for:

我们正在我们的团队中讨论 Java 的代码约定:

  • interface: Fooor IFooor FooInterface?

  • abstract: Fooor AbstractFoo?

  • Enums: Fooor FooEnum?

  • interfaceFooIFooFooInterface

  • abstract:Foo还是AbstractFoo

  • Enums:Foo还是FooEnum

I'm basically trying to put my personal preferences aside :) so reasons to back up one or other convention are very welcome.

我基本上是想把我的个人偏好放在一边:) 所以支持一个或其他约定的理由是非常受欢迎的。

采纳答案by Jon Skeet

In Java: Foo, AbstractFooand Foo- although AbstractFoocould just be Foo.

在 Java 中:Foo,AbstractFooFoo- 虽然AbstractFoo可能只是Foo.

Evidence:

证据:

  • java.util.List(interface)
  • java.util.AbstractList(abstract class)
  • java.util.Formatter.BigDecimalLayoutForm(enum)
  • java.util.List(界面)
  • java.util.AbstractList(抽象类)
  • java.util.Formatter.BigDecimalLayoutForm(枚举)

For the interface part, see the Naming Conventionssection of the Java Coding Conventions document. It doesn't talk about enums and abstract classes though.

对于接口部分,请参阅Java 编码约定文档的命名约定部分。虽然它没有谈论枚举和抽象类。

回答by JodaStephen

From my blog:

我的博客

  • Foo- The interface ultimately defines the concept, so it should have the best name.
  • AbstractFoo- An abstract implementation intended to be used as the base of a hierarchy of classes.
  • BaseFoo- An implementation intended to be used as the base of a hierarchy of classes, where the base class could be used on its own if necessary.
  • DefaultFoo- A "default" implementation that would be appropriate for the majority of typical use cases.
  • SimpleFoo- A "simple" implementation with no unexpected functionality, perhaps as an example or as a mock. A simple POJO would be a good "simple" implementation.
  • {Descriptive}Foo- Other implementations should describe what makes them unique.
  • Foo- 接口最终定义了概念,所以它应该有最好的名字。
  • AbstractFoo- 旨在用作类层次结构基础的抽象实现。
  • BaseFoo- 旨在用作类层次结构的基础的实现,如果需要,可以单独使用基类。
  • DefaultFoo- 适用于大多数典型用例的“默认”实现。
  • SimpleFoo- 一个没有意外功能的“简单”实现,可能作为示例或模拟。一个简单的 POJO 将是一个很好的“简单”实现。
  • {Descriptive}Foo- 其他实现应该描述是什么让它们独一无二。

The blog also discusses reasons against some of the other names.

该博客还讨论了反对其他一些名称的原因。

回答by jrharshath

interfaces: Foo

接口:Foo

Reason: Your code must not need to know that they are dealing with an interface. Writing 'IFoo' does just that. Instead, Foo makes it clear that 'Foo' is generic, and the object behind it may be a 'NumFoo' or a 'StrFoo'. The code really need not care.

原因:您的代码不必知道它们正在处理一个接口。编写“IFoo”就是这样做的。相反,Foo 明确表示 'Foo' 是泛型的,它背后的对象可能是一个 'NumFoo' 或一个 'StrFoo'。代码真的不需要关心。

abstract classes: AbstractFoo

抽象类:AbstractFoo

Reason: your code is never going to use this class directly. You will always subclass this class to make any classes that are used by other code. So it must be amply clear to a programmer that the class is an abstract one. And what better way to name it Abstract! Places where you need to use references of type AbstractFoo, you should reconsider using an interface instead. (Ofcourse, this is not possible in C++)

原因:你的代码永远不会直接使用这个类。您将始终对此类进行子类化以创建其他代码使用的任何类。所以程序员必须非常清楚这个类是一个抽象类。还有什么更好的方式将它命名为抽象!在需要使用 AbstractFoo 类型引用的地方,您应该重新考虑改用接口。(当然,这在 C++ 中是不可能的)

Enums: FooTypeor FooEnum. Personally, FooType is better because Type relates more easily to the "real world" that Enum does.

枚举:FooType或 FooEnum。就个人而言,FooType 更好,因为 Type 更容易与 Enum 所做的“现实世界”相关联。

Cheers!

干杯!

回答by Michael Borgwardt

No special conventions.

没有特别约定。

Having special naming conventions for these kinds of classes is basically a form of Hungarian notation (the bad kind): the information it gives you is already present in the syntax and is usually made easily available by IDEs e.g. when you hover over the name. Putting it into the name itself is pointless and ugly.

对这些类型的类具有特殊的命名约定基本上是一种匈牙利符号的形式(坏类型):它提供给您的信息已经存在于语法中,并且通常可以通过 IDE 轻松获得,例如当您将鼠标悬停在名称上时。把它放在名字本身是毫无意义和丑陋的。

Class names should simply describe the class's role as well as possible. This canlead to "natural" naming conventions - a very good example is the Java convention of naming interfaces with an -able suffix (Iterable, Comparable) - but I don't want to imagine the result if it were universally enforced and List, Map, etc. had to follow it.

类名应该尽可能简单地描述类的角色。这可能会导致“自然”命名约定——一个很好的例子是命名接口的 Java 约定,带有一个 -able 后缀(Iterable,Comparable)——但我不想想象如果它被普遍执行和 List 的结果,地图等不得不遵循它。

回答by dfa

My convention:

我的约定:

  • interface: Foo
  • abstract: it depends FooAdaptor or AbstractFoo or BaseFoo
  • enum: Foo or Foos
  • 接口: Foo
  • 抽象:这取决于 FooAdaptor 或 AbstractFoo 或 BaseFoo
  • 枚举:Foo 或 Foos

I really dislike using I in interface names or even FooInterface:

我真的不喜欢在接口名称甚至 FooInterface 中使用 I:

 interface FooInterface {

is like writing:

就像写:

 class FooClass {

or even:

甚至:

 abstract class AbstractFooClass {

it is simply prolix.

它简直是多产的。

回答by merxbj

Regarding the interfaces I personaly like:

关于我个人喜欢的接口:

Fooable

回答by cletus

My convention:

我的约定:

  • Interface:Foo;
  • Abstract:AbstractFoo;
  • Enum:usually Foobut in some circumstances FooType.
  • 接口:Foo;
  • 摘要:AbstractFoo;
  • 枚举:通常Foo但在某些情况下FooType

IFoois very .Net, not Java. FooInterfaceI've never seen used.

IFoo非常.Net,而不是Java。FooInterface没见过用过。

回答by Kissaki

About interfaces:

关于接口:

I prefer IFoo because it's a talking name, telling you it is an inferface right away. Second, for modules etc. where you do an interface for just one class, the class often has the same name as the interface. Then you can use Foo extends IFoo. Otherwise, well, you'd have to find a name. Or use FooInterface or whatever …

我更喜欢 IFoo 因为它是一个会说话的名字,告诉你它是一个接口。其次,对于只为一个类创建接口的模块等,该类通常与接口具有相同的名称。然后你可以使用 Foo 扩展 IFoo。否则,好吧,你必须找到一个名字。或者使用 FooInterface 或其他什么......

java.util.list as stated uses Foo. This is no problem as classes with different concepts implement it, thus already suggesting a different name (ArrayList, LinkedList …). I'm not quite sure if I really would prefer IList there. Dunno … :P

如前所述的 java.util.list 使用 Foo。这没有问题,因为具有不同概念的类实现了它,因此已经建议使用不同的名称(ArrayList、LinkedList ...)。我不太确定我是否真的更喜欢那里的 IList。不知道... :P

回答by ION Renalyte

Here's convetion used in my DEV team in ION.

这是我在 ION 的开发团队中使用的对流。

Interface

界面

interface IMyInterface

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::

abstract class MyAbstract

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::

enum EMyEnumeration

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::