简体   繁体   English

有没有一种方法可以将自定义类模板添加到Eclipse?

[英]Is there a way to add a custom class template to Eclipse?

Here's the scenario. 这是场景。 I often create classes that follow a certain pattern. 我经常创建遵循特定模式的类。 All classes extend a base abstract class (out of my control) with certain methods I wish to always override, including the constructor. 所有类都使用我希望始终覆盖的某些方法(包括构造函数)扩展了基本抽象类(超出我的控制范围)。 I'd like to rightclick on my package name, click New, and then click "Foo Class". 我想右键单击我的包名称,单击“新建”,然后单击“ Foo类”。 I'd then have a class added to my project, but using the "Foo" template instead of the standard class template. 然后,我将一个类添加到我的项目中,但是使用“ Foo”模板而不是标准类模板。 I understand I can change the class template, but I don't want to change it for all classes. 我知道我可以更改类模板,但是我不想为所有类都更改它。 Is this possible without writing a full blown extension? 如果不编写完整的扩展名,是否可以实现?

you can use the editor template to create a new template and use that. 您可以使用编辑器模板来创建新模板并使用它。

Go to Preferences->Java->Editor->Templates and create your template there. 转到“首选项”->“ Java”->“编辑器”->“模板”,然后在此处创建模板。

Then create a new class file and apply your template. 然后创建一个新的类文件并应用您的模板。

Unfortunately no. 抱歉不行。 What you'd like to achieve needs writing an eclipse plugin. 您想要实现的目标需要编写一个eclipse插件。

I'd suggest setting up a Template (at Preferences | Java | Editor | Templates) and giving it a short useful name describing your scenario. 我建议设置一个模板(在“首选项” |“ Java” |“编辑器” |“模板”中),并给它一个简短的有用名称来描述您的情况。 Whenever you'd want to write an additional template class, you could create a new class, press Ctrl+A, Del (select all and remove), then type "name Ctrl+Space" and have your template there as configured. 每当您想编写其他模板类时,都可以创建一个新类,按Ctrl + A,Del(全选并删除),然后键入“ name Ctrl + Space”,然后在那里配置您的模板。

ps. ps。 I'd really review your requirement, can't think of a valid approach at the moment which would require writing many similar classes without a valid way to remove the code duplication you are just about to introduce. 我真的会复查您的要求,目前还无法想到一种有效的方法,该方法需要编写许多类似的类而没有一种有效的方法来删除您将要引入的代码重复项。

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

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