简体   繁体   English

Java接口命名约定

[英]Java interface naming convention

When we write a java program with multiple classes, we name the java file similar to the name of the class with the main() method. 当我们编写具有多个类的Java程序时,我们使用main()方法将Java文件命名为类似于类的名称。 But when I write a java program consisting of multiple interfaces only, without any main() method within them, then what should be the name of the program? 但是,当我编写一个仅包含多个接口的Java程序,而其中没有任何main()方法时,那么该程序的名称应该是什么? Are there any specific rules of naming that program, or I may assign it any name xyz.java ? 命名该程序是否有任何特定规则,或者我可以为其指定任何名称xyz.java

There are code & naming conventions for everything in Java. Java中的所有代码都有代码和命名约定。

You can find most of them here: Code Conventions for the Java Programming Language 您可以在这里找到其中的大多数: Java编程语言的代码约定

A main(String[] args) method has nothing to do with the naming of the class and it's filename. main(String[] args)方法与类及其文件名的命名无关。

In fact, the file containing your code has to be identical with the name of the (top-level) class inside that file. 实际上,包含您的代码的文件必须与该文件中(顶级)类的名称相同。

I prefer not to use a prefix on interfaces: 我不想在接口上使用前缀:

  • The prefix hurts readability. 前缀会损害可读性。

  • Using interfaces in clients is the standard best way to program, so interfaces names should be as short and pleasant as possible. 在客户端中使用接口是进行编程的最佳标准方法,因此接口名称应尽可能简短和令人愉快。 Implementing classes should be uglier to discourage their use. 实施类应该更丑陋以阻止其使用。

  • When changing from an abstract class to an interface a coding 从抽象类更改为接口时,编码
    convention with prefix I implies renaming all the occurrences of the class --- not good! 带有前缀的约定,我暗示重命名该类的所有出现---不好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM