本文翻译自:Java Interfaces/Implementation naming convention [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

Interface naming in Java [closed] 11 answers Java中的接口命名[关闭] 11个答案

How do you name different classes / interfaces you create? 如何命名您创建的不同类/接口? Sometimes I don't have implementation information to add to the implementation name - like interface FileHandler and class SqlFileHandler . 有时我没有添加到实现名称的实现信息 - 比如接口FileHandler和类SqlFileHandler 。

When this happens I usually name the interface in the "normal" name, like Truck and name the actual class TruckClass . 当发生这种情况时,我通常将该界面命名为“普通”名称,例如Truck并命名实际的类TruckClass 。

How do you name interfaces and classes in this regard? 在这方面,您如何命名接口和类?

#1楼

参考:https://stackoom.com/question/BoG5/Java接口-实现命名约定-重复

#2楼

The standard C# convention, which works well enough in Java too, is to prefix all interfaces with an I - so your file handler interface will be IFileHandler and your truck interface will be ITruck . 标准的C#约定,在Java中运行良好,是为所有接口添加一个I - 所以你的文件处理程序接口将是IFileHandler ,你的卡车接口将是ITruck 。 It's consistent, and makes it easy to tell interfaces from classes. 它是一致的,并且可以很容易地从类中分辨出接口。

#3楼

Some people don't like this, and it's more of a .NET convention than Java, but you can name your interfaces with a capital I prefix, for example: 有些人不喜欢这样,它更像是.NET约定而不是Java,但你可以用大写字母I来命名你的接口,例如:

IProductRepository - interface

ProductRepository, SqlProductRepository, etc. - implementations

The people opposed to this naming convention might argue that you shouldn't care whether you're working with an interface or an object in your code, but I find it easier to read and understand on-the-fly. 反对这种命名约定的人可能会争辩说你不应该关心你是在使用代码中的接口还是对象,但我发现它更容易在运行中阅读和理解。

I wouldn't name the implementation class with a "Class" suffix. 我不会将实现类命名为“Class”后缀。 That may lead to confusion, because you can actually work with "class" (ie Type) objects in your code, but in your case, you're not working with the class object, you're just working with a plain-old object. 这可能会导致混淆,因为您实际上可以在代码中使用“类”(即Type)对象,但在您的情况下,您不使用类对象,您只是使用一个普通的对象。

#4楼

Name your Interface what it is. 为您的Interface命名它是什么。 Truck . Truck 。 Not ITruck because it isn't an ITruck it is a Truck . 不是ITruck因为它不是ITruck它是Truck 。

An Interface in Java is a Type . Java中的Interface是一种类型 。 Then you have DumpTruck , TransferTruck , WreckerTruck , CementTruck , etc that implement Truck . 然后你有implement Truck DumpTruck , TransferTruck , WreckerTruck , CementTruck等。

When you are using the Interface in place of a sub-class you just cast it to Truck . 当您使用Interface代替子类时,您只需将其转换为Truck 。 As in List . 与List 。 Putting I in front is just Hungarian style notation tautology that adds nothing but more stuff to type to your code. 把I放在前面只是匈牙利风格的符号重言式 ,它只会为你的代码添加更多东西。

All modern Java IDE's mark Interfaces and Implementations and what not without this silly notation. 所有现代Java IDE都标记了接口和实现,以及没有这种愚蠢符号的东西。 Don't call it TruckClass that is tautology just as bad as the IInterface tautology. 不要把它TruckClass ,它是重 TruckClass ,就像IInterface重言式一样糟糕。

If it is an implementation it is a class. 如果它是一个实现它是一个类。 The only real exception to this rule, and there are always exceptions, could be something like AbstractTruck . 这个规则唯一真正的例外,总是存在异常,可能就像AbstractTruck 。 Since only the sub-classes will ever see this and you should never cast to an Abstract class it does add some information that the class is abstract and to how it should be used. 由于只有子类才能看到这个,并且你永远不应该转换为Abstract类,它确实会添加一些类是抽象的信息以及它应该如何使用。 You could still come up with a better name than AbstractTruck and use BaseTruck or DefaultTruck instead since the abstract is in the definition. 您仍然可以提供比AbstractTruck更好的名称,并使用BaseTruck或DefaultTruck因为abstract在定义中。 But since Abstract classes should never be part of any public facing interface I believe it is an acceptable exception to the rule. 但由于Abstract类永远不应该成为任何面向公众的界面的一部分,我认为它是规则的可接受的例外。 Making the constructors protected goes a long way to crossing this divide. 使构造者protected还有很长的路要走。

And the Impl suffix is just more noise as well. 而Impl后缀也是更多的噪音。 More tautology. 更多的重言式。 Anything that isn't an interface is an implementation, even abstract classes which are partial implementations. 任何不是接口的东西都是一个实现,甚至是部分实现的抽象类。 Are you going to put that silly Impl suffix on every name of every Class ? 你打算在每个班级的每个名字上加上那个愚蠢的Impl后缀吗?

The Interface is a contract on what the public methods and properties have to support, it is also Type information as well. Interface是公共方法和属性必须支持的合同,它也是类型信息。 Everything that implements Truck is a Type of Truck . 实现一切Truck是一种类型的Truck 。

Look to the Java standard library itself. 查看Java标准库本身。 Do you see IList , ArrayListImpl , LinkedListImpl ? 你看到IList , ArrayListImpl , LinkedListImpl吗? No, you see List and ArrayList , and LinkedList . 不,你看到List和ArrayList ,以及LinkedList 。 Here is a nice article about this exact question. 这是一篇关于这个确切问题的好文章 。 Any of these silly prefix/suffix naming conventions all violate the DRY principle as well. 任何这些愚蠢的前缀/后缀命名约定都违反了DRY原则。

Also, if you find yourself adding DTO , JDO , BEAN or other silly repetitive suffixes to objects then they probably belong in a package instead of all those suffixes. 此外,如果您发现自己将DTO , JDO , BEAN或其他愚蠢的重复后缀添加到对象中,那么它们可能属于一个包而不是所有这些后缀。 Properly packaged namespaces are self documenting and reduce all the useless redundant information in these really poorly conceived proprietary naming schemes that most places don't even internally adhere to in a consistent manner. 正确打包的命名空间是自我记录的,并且减少了这些非常缺乏构思的专有命名方案中的所有无用的冗余信息,大多数地方甚至没有以一致的方式内部遵守这些命名方案。

If all you can come up with to make your Class name unique is suffixing it with Impl , then you need to rethink having an Interface at all. 如果所有你能想到的,你的Class名称是唯一的,那么就用Impl作为后缀,那么你需要重新考虑一个Interface 。 So when you have a situation where you have an Interface and a single Implementation that is not uniquely specialized from the Interface you probably don't need the Interface . 因此,当您遇到一个Interface和一个不是Interface专用的Implementation ,您可能不需要Interface 。

#5楼

TruckClass sounds like it were a class of Truck , I think that recommended solution is to add Impl suffix. TruckClass听起来像是一类Truck ,我认为推荐的解决方案是添加Impl后缀。 In my opinion the best solution is to contain within implementation name some information, what's going on in that particular implementation (like we have with List interface and implementations: ArrayList or LinkedList ), but sometimes you have just one implementation and have to have interface due to remote usage (for example), then (as mentioned at the beginning) Impl is the solution. 在我看来,最好的解决方案是在实现名称中包含一些信息,在特定实现中发生了什么(就像我们使用List接口和实现: ArrayList或LinkedList ),但有时你只有一个实现,并且必须有接口到期远程使用(例如),然后(如开头所述) Impl是解决方案。

#6楼

The name of the interface should describe the abstract concept the interface represents. 接口的名称应描述接口所代表的抽象概念。 Any implementation class should have some sort of specific traits that can be used to give it a more specific name. 任何实现类都应具有某种特定的特征,可用于为其提供更具体的名称。

If there is only one implementation class and you can't think of anything that makes it specific (implied by wanting to name it -Impl ), then it looks like there is no justification to have an interface at all. 如果只有一个实现类,你不能想到任何使它具体化的东西(暗示想要命名它-Impl ),那么看起来没有理由拥有一个接口。

java 接口方法名重复_Java接口/实现命名约定[重复]相关推荐

  1. java接口方法默认权限_java 接口内定义方法的权限是什么?

    画数据流图和用例图 某高校欲开发一个成绩管理系统,记录并管理所有选修课程的学生的平时成绩和考试成绩,其主要功能描述如下: 1.每门课程都有3到6个单元构成,每个单元结束后会进行一次测试,其成绩作为这门 ...

  2. java实体类实现抽象类_java接口、抽象类、实体类关系介绍

    接口:接口是一个引用类型,与类相似,不过在接口中只能包含常量,方法名和嵌套类型.没有构造器,所以不能被实例化只能被类所实现或者被另外的接口所继承.在接口中声明的方法没有方法体. 抽象类:将类层次中共有 ...

  3. java端口是多少钱_Java 接口,最少必要知识

    1.接口的基本概念 在 Java 中,被关键字 interface 修饰的"类"是接口. 接口的定义如下: interface 接口名称{ 全局常量: 抽象方法: } 2. 接口的 ...

  4. java接口有非抽象方法_Java接口、抽象方法

    1)Java接口(Interface),是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能) ...

  5. Java接口属性值定义_Java接口(interface)的定义和使用

    Java-接口interface定义和使用 1.什么是接口 Java为单继承,当父类的方法种类无法满足子类需求时,可实现接口扩容子类能力. 即:Java中使用抽象类/父类表示通用属性时,每个类只能继承 ...

  6. java 接口校验接收参数_java接口参数校验

    (可扩充) 命令编号 命令名称 参数命令 提取现场机时间 上传现场机时间 设置现... 接口标准> (以下简称"接口标准" ) <数据对接接口校验规则> < ...

  7. java接口可以扩展抽象类_Java—接口与抽象类

    1.语法层面上的区别 1)抽象类可以提供成员方法的实现细节,而接口中只能存在public abstract 方法: 2)抽象类中的成员变量可以是各种类型的,而接口中的成员变量只能是public sta ...

  8. java对外查询接口注意的地方_Java接口注意点

    1.接口可以多实现:一个实现类可以同时实现多个接口 package com.qf.demo02_interface; //定义一种规则: interface A{ public void testA( ...

  9. java 方法名相同_Java的方法的重载 :方法名相同,参数类型不同 - Break易站

    人类设计语言时,相同的词汇可以表达多种意思.而在Java里面,方法也被设计成这个模式,而区分这些相同方法名的就是方法的参数. Java的方法的重载的特点 方法的重载有下面的特点: 1. 方法名相同,参 ...

最新文章

  1. JUNOS Olive GRE Tunnel Configuration
  2. 动态规划算法——最长上升子序列
  3. 推荐:解析“extern”
  4. 2016大数据发展7大趋势
  5. Mr.J-- jQuery学习笔记(九)--事件绑定移除冒泡
  6. LINQ Enumerable 续
  7. 数据挖掘:特征提取——PCA与LDA
  8. 设定MyEclipse编辑代码区域文字的大小及非关键字的字体、字形和颜色
  9. Nutch数据集的目录具体内容
  10. Python:安装pip
  11. opencv实现摄像头的实时人脸识别
  12. 输出希腊字母表java
  13. 【无中生有】---14---用户行为监控系统嵌入
  14. 博客外链应该要怎么养
  15. iconfont(图标字体)
  16. 段子用计算机等于250,段子手要失业了,计算机也懂幽默
  17. windows禁用屏幕旋转_如何在Windows 10中禁用屏幕自动旋转
  18. 利用勾子监视系统或进程中的各种事件消息,截获发往目标窗口的消息并进行处理
  19. 软件测试学习心得-6
  20. Mysql 多列合并为一列

热门文章

  1. all在java语言什么意思_this什么意思
  2. echarts折现图的配置
  3. 实习与就业--软件外包公司(一)
  4. PHP反射ReflectionClass、ReflectionMethod
  5. php性别类型是什么,专属男女的各种性别符号
  6. 【亲测】登录界面,拖动滑块验证
  7. HttpClient 模拟登录手机版新浪微博
  8. 【多人在线游戏架构实战-基于C++的分布式游戏编程】开篇
  9. 诛仙手游服务器购买无限制,全新福利提升 摆摊amp;购买增加次数限制_18183诛仙手游专区...
  10. 商朝·商高·勾三股四玄五·勾股定理