How does eclipse extract and encapsulate a piece of code?


When developing eclipse, if you don’t extract the program, you need to modify it everywhere, which is extremely inconvenient and prone to errors. After encapsulation, extraction is different. It can be called and modified. It is very simple and convenient. How to extract? Now let’s take a look at the detailed tutorial.

Software name:
Official Chinese version of eclipse classic 4.4.2
Software size:
Update time:
2015-10-19Download now

1. Use eclipse to open an existing project code or create a new test project code. Select the code block to extract the encapsulation method and right-click to select itRefactor. Choose againExtract Method, shortcut key(Alt+Shift+M

2. Fill in the name of the method and select the access rights,The default is privateOf

3. Click preview to view the preview effect. In the preview page, you can see the newly generated method after the extraction method and the code to call the new method

4. There’s no problem with the preview,Click back to returnThen click OK to extract the encapsulation method. Eclipse generated extraction method code for us, remember to check to see if it meets our needs

5、The above is the extraction method without parametersThe following describes the case of parameters. At this time, a parameter list will appear below the method name. By default, the required parameters will be automatically checked and filled in for us. If they do not meet the requirements, the parameter list will be filled in,You can click Edit to edit

6. Then click preview to see the effect, no problemIf you have a problem, click OK to start the extractionAfter generation, you can see that the method with parameters is automatically generated, and the required parameters are also called. This is the end of the extraction method. Is it much more efficient than our direct manual encapsulation method.

The above is the eclipse extraction encapsulation tutorial, I hope you like it, please continue to pay attention to developer.

Related recommendations:

How does eclipse configure the sublime text editor?

How to set eclipse automatic typesetting to not truncate long lines of code?

How do eclipse and MyEclipse find the specified code in a project?