我们可以有一个“不重写具体方法......”的方法吗?实现接口时编译时错误?
在实现接口时,我们会出现“不重写具体方法...”编译时错误吗?
一个更清楚的例子:
我建立了一个包含接口的框架。要使用该框架,开发人员需要实现一些接口。 但如果他们不重写 equals(Object object) 和 hashCode()Object 方法,内部 API 逻辑将被破坏!
javadoc 中提到的所有内容,但我希望在实现接口而不覆盖某些具体方法时出现编译时错误或者运行时异常强>。
Can we have a "not override a concrete method ..." compile time error when implementing interfaces ?
An example to be more clear :
I build up a framework containing interfaces. To use the framework developers need to implements some interfaces.
But if they don't override equals(Object object)
and hashCode()
Object
methods the internal API logic will be broken !!
All that is mentioned in the javadoc but I want to have a compile time error or maybe a runtime exception when an interface is implemented without overriding some concretes methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 Java 6 开始,Java 附带注释处理器功能: 源代码分析。 (从技术上讲,它是 Java 5 的一部分,但 Java 6 将其集成到编译器阶段而不是一个特殊的工具)。 Sun 提供了此入门指南。
编译器在构建项目时会调用注释处理器,并且可能会像编译器一样发出错误。这是一种执行比 Java 指定的更多规则的巧妙方法。
下面是一个示例处理器,它将执行您想要的检查:
注释处理器可以直接在编译器选项中指定(通过将
-processor com.notnoop.CheckMethodOverride
添加到编译器选项),或添加将META-INF/services
文件添加到包含处理器名称的类路径 (com.notnoop.CheckMethodOverride
)。文件夹META-INF
可以位于项目的根源目录中(如果使用 maven,则为资源目录)。这适用于任何命令行编译器。我不知道如何在 IDE 中激活它。
Java ships with annotation processor capabilities, starting from Java 6: Source Code Analysis. (Technically it's part of Java 5, but Java 6 integrated it into the compiler phase rather than a special tool). Sun provides this Getting Started guide.
An annotation processor gets invoked by the compiler while building the project, and may issue errors just like the compiler does. It's a neat way to enforce more rules than Java specifies.
Here is a sample Processor that would perform the check you desired:
The annotation processors can be specified directly in the compiler options (by adding
-processor com.notnoop.CheckMethodOverride
to the compiler options), or adding aMETA-INF/services
file to your classpath which contains the name of the processor (com.notnoop.CheckMethodOverride
). The forlderMETA-INF
can live in the root source directory of the project (or resource directory if using maven).This works with any command line compiler. I have no idea how to activate it in the IDEs though.
您不能使用接口来做到这一点,但可以使用抽象类来做到这一点。例如
,告诉开发人员只要想要实现
Entity
就扩展AbstractEntity
。如果您确实希望强制执行该规则,则需要某种运行时检查,例如 Spring 中的 bean 后处理器。
You can't do that with interfaces, but you can do that with abstract classes. E.g.
and tell developers to extend
AbstractEntity
whenever they want to implement anEntity
.If you really want the rule to be enforced you'd need some sort of runtime check, like a bean post-processor in Spring.
恐怕SO是问错地方了。我们回答有关语言的问题,但我们不创造语言。
Sun 有一个社区流程,您可以在其中提出改进建议或直接报告错误。如果您愿意,我可以为您挖掘链接。
但我怀疑这件事不会完成。如果到目前为止它还没有被认为是一个好主意,那么它可能就不再是了。
从短期来看,可以帮助您的东西可能是独立的样式检查程序。
我想到了 CheckStyle,它可能是最大的名字,它像啤酒一样免费并且提供丰富的选择。我怀疑,但我不确定,“不实现
hashCode()
和equals()
”也在其中的某个地方。I'm afraid SO is the wrong place to ask. We answer questions about languages, we don't make the languages.
Sun has a Community Process in place where you can make suggestions for improvement or outright bug reports. I can dig you up links if you like.
I suspect it won't get done, though. If it wasn't considered a good idea until now, it probably won't be.
Something to help you in the short run might be standalone style checking programs.
CheckStyle comes to mind, it's probably the biggest name out there, it's free-as-in-beer and offers a WEALTH of options. I suspect, but I'm not sure, that "not implementing
hashCode()
andequals()
is somewhere in there too.我只是想到了一个可能的替代方案:
您的接口(或可能是抽象方法)可以指定方法(例如)
equals2()
和hashCode2()
。当然,这些对于接口的任何实现者来说都是强制性的。因此,您可以强迫您的图书馆使用者编写一些东西。然后,与接口的实现者一起工作的库类可以使用这些“自定义”函数作为“真实”函数的替换或实现,并重新定义本身。
这不是一个非常优雅的概念,也不是很具体,但你也许可以利用它做点什么。
I just thought of a possible alternative:
Your interface (or possibly abstract methods) could specify methods (e.g.)
equals2()
andhashCode2()
. These would be mandatory to implement to any implementor of the interface, of course. You could thus force your library consumers to code something.And your library classes, that work with implementors of your interface, could then use these "custom" functions as replacements or implementations of the "real" functions, and to the re-definition themselves.
It's not a very elegant concept, and not very fleshed out, but you may be able to make something of it.