java api中接口命名的标准是什么

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

What is standard for Interface naming in java api

javajava-api

提问by vijay.shad

I am asked by one of colleague about the Throwableclass in java API.

一位同事问我有关ThrowableJava API 中的类的问题。

As per standard, I do understand, every word ending *able is a interface in java API. There is a industry standard about using such words as Interface names. So, I unknowingly, told him about this as base interface for all the exception and error types in java world. Then he shows me the java file for this class.

根据标准,我确实理解,以 *able 结尾的每个单词都是 Java API 中的一个接口。有一个关于使用诸如接口名称之类的词的行业标准。所以,我在不知不觉中告诉他这是 Java 世界中所有异常和错误类型的基本接口。然后他向我展示了这个类的 java 文件。

My questions:

我的问题:

  1. Why java people has choosen this name to be a class. I think this should have been a interface by default?

  2. Is this a pattern to use *able words as interface?

  3. Is there any other example of class ending with *able?

  1. 为什么 java 人选择这个名字作为一个类。我认为这应该是默认的界面?

  2. 这是使用*able 词作为界面的模式吗?

  3. 有没有其他以 *able 结尾的类的例子?

Regards.

问候。

回答by Daniel Alexiuc

Nouns are always used to name classes, but Throwable is an exception.

名词总是用来命名类,但 Throwable 是一个例外。

(See what I did there?)

(看看我在那里做了什么?)

回答by wkl

It's very common for those '-able' names to be interfaces in Java, but there is no official convention for interface naming that I've found that suggests that '-able' names should be interface names, though typically that is the case.

在 Java 中,那些“-able”名称是接口是很常见的,但是我发现没有正式的接口命名约定表明“-able”名称应该是接口名称,尽管通常情况如此。

Official Java naming conventions can be found here - it's pretty lean, there really aren't any restrictions for class or interface naming:

官方 Java 命名约定可以在这里找到 - 它非常精简,对于类或接口命名确实没有任何限制:

As to your Throwablequestion, James Gosling once answered why it's a class rather than an interface, even though the name was more fitting for an interface.

至于你的Throwable问题,James Gosling 曾经回答过为什么它是一个类而不是一个接口,尽管这个名字更适合接口。

Unfortunately, the original article from Sun/Oracle's site has vanished into the internet ether, so I can only provide indirect attribution:

不幸的是,Sun/Oracle 网站上的原始文章已经消失在互联网上,所以我只能提供间接归属:

edit: Since I continue to get upvotes to this question, I found the link to the Sun discussion via the Wayback Machine, here: http://web.archive.org/web/20071013225816/http://java.sun.com/features/2002/03/gosling.html?source=jdc_news&date=20020430

编辑:由于我继续对这个问题进行投票,我通过Wayback Machine找到了 Sun 讨论的链接,这里:http: //web.archive.org/web/20071013225816/http://java.sun.com /features/2002/03/gosling.html?source=jdc_news&date=20020430

JDC: Why is Throwable not an interface? The name kind of suggests it should have been. Being able to catch for types, that is, something like try{}catch (), instead of only classes. That would make the Java programming language much more flexible.

JG: The reason that the Throwable and the rest of those guys are not interfaces is because we decided, or I decided fairly early on. I decided that I wanted to have some state associated with every exception that gets thrown. And you can't do that with interfaces; you can only do that with classes.The state that's there is basically standard. There's a message, there's a snapshot, stuff like that that's always there. and also, if you make Throwable an interface the temptation is to assign, to make any old object be a Throwable thing. It feels stylistically that throwing general objects is probably a bad idea, that the things you want to throw really ought to be things that are intended to be exceptions that really capture the nature of the exception and what went on. They're not just general data structures.

JDC:为什么 Throwable 不是接口?这个名字暗示它应该是。能够捕捉类型,即像 try{}catch () 之类的东西,而不仅仅是类。这将使 Java 编程语言更加灵活。

JG:Throwable 和其他人不是接口的原因是因为我们决定,或者我很早就决定了。我决定让一些状态与抛出的每个异常相关联。而你不能用接口来做到这一点;你只能用类来做到这一点。那里的状态基本上是标准的。有一条消息,有一张快照,诸如此类的东西一直存在。而且,如果你让 Throwable 成为一个接口,那么诱惑就是分配,让任何旧的对象成为一个 Throwable 的东西。从风格上来说,抛出一般对象可能是一个坏主意,你想要抛出的东西真的应该是旨在成为真正捕捉异常本质和发生了什么的异常的东西。他们'

回答by Brad Mace

There are others such as

还有其他的比如

And of course there are plenty of interfaces that don't end in -able. Some people like to prefix all their interface names with an 'I' (IAdjustableinstead of Adjustable). Like code formatting wars, their isn't universal agreement. Sun has some suggestionsbut they are pretty vague.

当然,还有很多接口不是以 in- able结尾的。有些人喜欢在所有接口名称前加上“I”(IAdjustable而不是Adjustable)。就像代码格式大战一样,它们并不是普遍共识。Sun 提出了一些建议,但它们非常含糊。

回答by Ben Holland

  1. Interface names should always be an adjective and if possible should end in "able" has been the trend in naming conventions within the Java community. That does not have to be followed strictly, its just a naming convention, there is nothing to stop you from naming the interface/class whatever you want.

  2. Yes, see http://www.iwombat.com/standards/JavaStyleGuide.html#Class%20and%20Interface%20Names

  3. Clonable

  1. 接口名称应该始终是一个形容词,如果可能的话,应该以“able”结尾,这是 Java 社区命名约定的趋势。这不必严格遵守,它只是一个命名约定,没有什么可以阻止您随意命名接口/类。

  2. 是的,请参阅http://www.iwombat.com/standards/JavaStyleGuide.html#Class%20and%20Interface%20Names

  3. 可克隆

*Note, Interface names should generally be adjectives, whereas class names should generally be nouns.

*注意,接口名称通常应该是形容词,而类名称通常应该是名词。

Check out page 15 of this style guide document released by Sun. http://java.sun.com/docs/codeconv/CodeConventions.pdf

查看 Sun 发布的这份风格指南文档的第 15 页。 http://java.sun.com/docs/codeconv/CodeConventions.pdf

There is also some debate as to if it is a good style to add the letter I to interfaces. (Example: ICat, IDog, etc.), but that generally applies to other languages and not to Java per say.

关于在接口中添加字母 I 是否是一种好的风格,也存在一些争论。(例如:ICat、IDog 等),但这通常适用于其他语言,而不是 Java。

In my personal opinion a convention is just a "rule of thumb", if the convention gets in the way of the readability of your code, go with a more descriptive interface/class name instead of the one that follows convention, but if your really struggling to come up with a good descriptive class name, maybe you need to think about the functionality of your class/interface again a bit more.

在我个人看来,约定只是一个“经验法则”,如果约定妨碍了代码的可读性,请使用更具描述性的接口/类名称而不是遵循约定的名称,但如果您真的努力想出一个好的描述性类名,也许你需要再考虑一下你的类/接口的功能。

回答by Kirk Woll

We are talking about naming conventionshere and yes, *able is the preferred convention for naming interfaces. As I'm sure you've seen, there are always exceptions. For example, System.arraycopyis not camel cased.

我们在这里谈论命名约定,是的,*able 是命名接口的首选约定。我相信你已经看到了,总有例外。例如,System.arraycopy不是骆驼壳。