英文:
Is it possible to automatically add an import in all new classes in Eclipse?
问题
我有一个包含将在许多类中使用的方法的类。我想知道是否可能让Eclipse在我创建新类时自动导入该类,以方便起见。谢谢!
英文:
I have a class with methods that will be used in many classes. I was wondering if it is possible to have Eclipse automatically import that class when I create a new one for convenience's sake. Thanks!
答案1
得分: 3
我知道这个答案并没有专门解答你的问题,但我觉得有些人可能希望使用这个解决方案,而不是提供的那个解决方案。
与其用期望的导入方式创建所有文件,你可能想要更灵活一点,但仍然不必手动添加所有所需的导入。
在这种情况下,你可以考虑使用 Java->Editor->Templates 功能。通过这个功能,你可以定义类似这样的内容:
(图片链接已省略)
当你想要插入这个模板时,只需在想要插入的位置输入 testimports
,然后按下 Ctrl+Space。
如果你有一些不同的导入块的变体,这将为你提供这种灵活性。
英文:
I know this answer doesn't specifically address the question, but I felt that some people might want this solution instead of the one given.
Instead of creating all files with the imports you expect, you might want a little more flexibility, but still without having to manually add all the imports you need.
In that case, you might consider instead using the Java->Editor->Templates feature. With this, you could define something like this:
When you want to insert the template, you just type testimports
where you want them and press <kbd>Ctrl</kbd>+<kbd>Space</kbd>.
If you have some variations of which blocks of imports you need, this will give you that flexibility.
答案2
得分: 1
你可以在菜单选项 Window -> Preferences -> Java -> Code Style -> Code Templates
中点击相应模板(在你的情况下是 New Java
文件),修改类主体模板,并查看在模板中添加导入语句并创建新类时会发生什么。
英文:
You can modify the class body template at the menu option Window -> Preferences -> Java -> Code Style -> Code Templates
by clicking the Edit button of the corresponding template (in your case, New Java
files) and see what happens when you add the import statement to the template and create a new class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论