开发者

How do I easily add a new method in Eclipse?

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.

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.

Is there a w开发者_C百科ay to do this?


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


In addition to 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.

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 voila your method gets added. Even without using a bloated dialog. Makes sense in a perfect world, where we work testdriven. :)


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.

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, Papyrus or MDT-UML2Tools might 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜