简体   繁体   English

在Eclipse的部分代码中重构参考变量

[英]Refactor a reference variable in part of code in Eclipse

How would I go about renaming a variable in only part of the code? 我将如何仅在部分代码中重命名变量?

For example: 例如:

System.out.println("Rectangle 1: " + "\n" + "Width: " + r1.width + "\n" + "Height: " +
        r1.height + "\n" + "Color: " + r1.color + "\n" + "Area and Perimeter: " + 
                r1.getArea(r1.width, r1.height) + ", " + r1.getPerimeter(r1.width, r1.height));

So if I want to type out the same for a second rectangle using r2 as the refVar, is there a way I can quickly do this? 因此,如果我想使用r2作为refVar在第二个矩形中键入相同的内容,是否可以快速执行此操作? I tried copy and pasting then using Alt + Shift + R, but, it ends up changing all of the r1 refvars. 我尝试了复制和粘贴,然后使用Alt + Shift + R,但是最终更改了所有r1 refvar。

I suggest you to use Find/Replace dialog for this problem it suites for your need. 我建议您使用“ 查找/替换”对话框来解决此问题,它适合您的需要。 Select a set of statements then press Ctrl + F . 选择一组语句,然后按Ctrl + F。 A Find/Replace dialog will popup, note that in Scope group Selected lines option is selected. 将弹出“查找/替换”对话框,请注意,在“ 范围组”中的“ 选定的行”选项中已选中。

You can use Replace All or Replace/Find . 您可以使用全部 替换替换/查找 But be careful that it also replaces the string in comments if found. 但是请注意,如果找到它,它还会替换注释中的字符串。

Refer picture below. 请参考下面的图片。

在此处输入图片说明

Take a look at don't repeat yourself (DRY) principle which was designed to prevent situations like yours. 看看不要重蹈覆辙(DRY)的原则,该原则旨在防止类似您的情况。 Instead of renaming variables create separate method which will accept any Rectangle and print its details. 无需重命名变量,而是创建单独的方法,该方法将接受任何Rectangle并打印其详细信息。

public static void printRectangleDetails(Rectangle r){

    System.out.println("Rectangle 1: " + "\n" + "Width: " + r.width + "\n" + "Height: " +
            r.height + "\n" + "Color: " + r.color + "\n" + "Area and Perimeter: " + 
                    r.getArea(r.width, r.height) + ", " + r.getPerimeter(r.width, r.height));
}

Now you can use it with your r1 and r2 rectangles when needed 现在,您可以根据需要将其与r1r2矩形一起使用

printRectangleDetails(r1);
...
printRectangleDetails(r2);

If for some reason where you can't create separate method and use DRY principle you can do something like this: 如果由于某种原因您无法创建单独的方法并使用DRY原理,则可以执行以下操作:

lets say we have 可以说我们有

String foo = "foo";
System.out.println(foo+" hello wordls"+ foo);

String bar = "bar";
System.out.println(foo+" hello wordls"+ foo);

and you want to replace foo in second print statement to bar . 并且您想将第二个打印语句中的foo替换为bar Using Alt + Shift + R (or from menu: Refactor -> Rename.. ) on second printing statement would rename all foo references . 在第二条打印语句上使用Alt + Shift + R (或从菜单: Refactor > Rename.. )将重命名所有foo引用。 To prevent it redeclare your foo reference (compiler will give you error, but don't worry, we will later remove it, it is helpful only while renaming process) just before statements from which you want to change foo to bar like 为了防止它在您要将foo更改为bar语句之前,重新声明您的foo引用(编译器会给您带来错误,但请放心,我们稍后会删除它,仅在重命名过程中才有用)。

String foo = "foo";
System.out.println(foo+" hello wordls"+ foo);

String bar = "bar";
String foo = "whatever";
//     ^^^
System.out.println(foo+" hello wordls"+ foo);

Now use Alt + Shift + R on this new duplicate foo and eclipse will look for foo from this new reference, and ignore earlier foo s, so you should be able to see something like 现在在这个新的重复foo上使用Alt + Shift + R ,eclipse将从这个新引用中查找foo ,并忽略早期的foo ,因此您应该可以看到类似

在此处输入图片说明

(as you can see first two foo are not selected for renaming) (如您所见,未选择前两个foo重命名)
so you can change it to bar like 所以你可以将其更改为bar

String foo = "foo";
System.out.println(foo+" hello wordls"+ foo);

String bar = "bar";
String bar = "whatever";
System.out.println(bar+" hello wordls"+ bar);

and after that just removed this additional String bar = "whatever"; 然后,删除该附加的String bar = "whatever"; since you don't need it any more. 因为您不再需要它了。

BUT BE CAREFUL. 不过要小心。 This way you will rename all foo variables after duplicate foo , not only those in System.out.println(foo+" hello wordls"+ foo); 这样,您将在重复的foo之后重命名所有foo变量,不仅是System.out.println(foo+" hello wordls"+ foo); which you wanted to rename. 您想重命名。 To make sure you are not changing anything you don't want to, place code you want to change at the end of your method (where you are sure there is no foo which shouldn't be changed after it). 为了确保您没有更改任何您不想更改的代码,请将要更改的代码放在方法的末尾(您可以确定那里没有foo ,之后不应该对其进行更改)。 After you are done move your changed code to place you want. 完成后,将更改的代码移至所需位置。

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

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