如何在 Eclipse 中轻松添加新方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1849656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I easily add a new method in Eclipse?
提问by skiphoppy
A coworker of mine new to Eclipse (from JBuilder) wants to know how to just click on a class and add a new method. I right-clicked on the class in the Outline view, and sure enough, there's no "new method" command. I can generate getters and setters, and delegate methods, and extract methods, and pull methods up or push them down, all of which are very useful, but I don't see how to create a new method.
我的一个 Eclipse 新手(来自 JBuilder)想知道如何只单击一个类并添加一个新方法。我在 Outline 视图中右键单击该类,果然,没有“新方法”命令。我可以生成getter 和setter,委托方法,提取方法,上拉或下推方法,所有这些都很有用,但我不知道如何创建新方法。
I can type private (or public), with no space, and access code-assist with control-space, and create a new method, and then I can right-click on that method and change the method signature from the refactor menu (alt-shift-C), but it seems like there should be a nice way to click on a class somehow and bring up that method signature wizard right away for creating a brand new method.
我可以键入 private(或 public),没有空格,并使用控制空间访问代码辅助,并创建一个新方法,然后我可以右键单击该方法并从重构菜单中更改方法签名(alt -shift-C),但似乎应该有一种很好的方法来以某种方式单击一个类并立即调出该方法签名向导以创建一个全新的方法。
Is there a way to do this?
有没有办法做到这一点?
回答by Turismo
The closest you can get to what you describe is
1. type the method name (no modifier or something else)
2. hit Ctrl-1 -> create method stub
3. hit Alt-Shift-C to change the signature
最接近您描述的内容是
1. 键入方法名称(无修饰符或其他内容)
2. 按 Ctrl-1 -> 创建方法存根
3. 按 Alt-Shift-C 更改签名
回答by gia
In addition to Turismo:
除了 Turismo:
Even though you should work TDD these days, your starting point might be the Testcase anyway. So while you at it to define the behaviour of your instances, you do as he suggested.
尽管这些天您应该使用 TDD,但无论如何您的起点可能是测试用例。因此,当您定义实例的行为时,您可以按照他的建议进行操作。
void someTest() {
MyClass myInstance = new MyClass();
myInstance.theNewMethodGoesHere();
}
And because you haven't added "theNewMethodGoesHere" the compiler will mark that thing as undefined. Then you'll use the ctrl+1 key and voilayour method gets added. Even without using a bloated dialog. Makes sense in a perfect world, where we work testdriven. :)
并且因为您没有添加“theNewMethodGoesHere”,编译器会将该内容标记为未定义。然后你将使用 ctrl+1 键,瞧你的方法被添加了。即使不使用臃肿的对话框。在我们以测试驱动的方式工作的完美世界中是有意义的。:)
回答by VonC
JDT being one of the main plugins bundled with eclipse (which is an application platform on top of the OSGi-based equinox), it is mainly a Java editor.
Meaning: to create a new method... you just type it in a Java editor.
JDT 是与 eclipse(它是基于 OSGi 的 equinox 之上的应用程序平台)捆绑的主要插件之一,它主要是一个 Java编辑器。
意思是:要创建一个新方法……您只需在 Java 编辑器中键入它。
The only other eclipse environment where you see that kind of feature (click on a class and type new methods) is a UML-oriented editor, like the Omondo product.
(In the UML2 project, Papyrusor MDT-UML2Toolsmight provide that sort of editing capabilities eventually)
But aside that kind of plugin, I do not think you can achieve natively this way of creating new methods.
唯一可以看到这种功能(单击类并键入新方法)的其他 Eclipse 环境是面向 UML 的编辑器,例如Omondo 产品。
(在UML2项目,纸莎草或MDT-UML2Tools可能提供那种编辑功能的最终)
但是,除了那种插件,我不认为你可以本地实现创造新方法,这种方法。