IntelliJ 是否可以将 @Overrides 添加到特定接口的所有方法?
我创建了一个接口,包含大约 30 个方法并在 30 个类中实现。
我想将 @Override 添加到每个实现中,但我不想手动完成。
IntelliJ 如何帮助我?
界面如下:
public interface PreviewObjectTests {
void testGetName();
void testGetType();
//... 30 similar methods
}
实现代码:
public class PreviewObjectAccountTest implements PreviewObjectTests {
//I want to add @Override here!
@Test public void testGetName() {
assertThat(...);
}
//I want to add @Override here!
@Test public void testGetType() {
assertThat(...);
}
//...30 similar methods
}
I have created an interface, with about 30 methods and implemented in 30 classes.
I want to add @Override to each of the implementations, but i would like not to do it by hand.
How can IntelliJ help me?
interface looks like this:
public interface PreviewObjectTests {
void testGetName();
void testGetType();
//... 30 similar methods
}
implementation code:
public class PreviewObjectAccountTest implements PreviewObjectTests {
//I want to add @Override here!
@Test public void testGetName() {
assertThat(...);
}
//I want to add @Override here!
@Test public void testGetType() {
assertThat(...);
}
//...30 similar methods
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 Alt+Enter 意图即可轻松完成,然后按向右箭头进入子菜单:
Easily done with Alt+Enter intention, then press Right arrow for the sub-menu:
对于 IntelliJ:
For IntelliJ:
我找到了一种使用简单的查找/替换工具来完成此操作的方法。
这适用于我的特定情况,因为实际上没有任何其他方法类文件。我假设如果我在属于其他接口的文件中有很多方法,这会更疯狂
I found a way to do it with the simple find/replace tool.
This works in my particular case, because there were not really any other methods in the class files. I will assume this will be a bit more crazy if i had a lot of methods in the files belonging to other interfaces
它不是日食或特定想法。它的编译器特定以及您遵守的合规性。
在日食中它是 1.6 或更高。
在想法上我相信它是 1.5 或更高。
在上述情况下,相应的 IDE 会自动检查。
Its not eclipse or idea specfic. Its compiler specfic and what compliance you adhere to.
In eclipse its 1.6 or higher.
And In idea I believe its for 1.5 or higher.
In the above cases it will bechecked automatically by respective IDEs.
对于 Eclipse(在 Mars.2 上测试):
窗口/首选项(或项目属性)
Java/编译器
错误/警告
注释
缺少 @Override 注释 - 设置为错误(或警告)
重建工作区后,在“标记”视图中,右键单击其中一个错误,“快速修复”(Ctrl+1),然后应该会显示所有错误。选择全部并应用修复。
这使我能够正确注释数百个类......
For Eclipse (tested on Mars.2):
Window / Preferences (or Project Properties)
Java / Compiler
Error/Warnings
Annotations
Missing @Override annotation - Set to Error (or Warning)
After the rebuild workspace, in the Markers view, right click on one of the errors, Quick Fix (Ctrl+1) and all the errors should appear. Select all and apply the fix.
This worked for me to properly annotate a few hundreds of classes...