简体   繁体   English

如何仅重构Android Studio中的某些区域?

[英]How to refactor only certain area in Android Studio?

How would I refactor only certain areas of code in Android Studios that are commonly called? 在Android Studio中,我该如何仅重构某些代码区域? For instance: 例如:

Dog dog = new Dog();
dog.bark()

Dog dog = new Dog();
dog.bark()

Dog dog = new Dog();
dog.bark()

Dog dog = new Dog();
dog.bark()

I want to be able to change the 2nd copy of the dog object into dog1 and use dog1.bark instead of dog.bark(). 我希望能够将dog对象的第二个副本更改为dog1,并使用dog1.bark而不是dog.bark()。 I also want to rename the 3rd dog object to dog3 and call dog3.bark instead. 我也想将第三个dog对象重命名为dog3,而是调用dog3.bark。 Is there any shortcut that allows me to rename only certain portions of code in android studios? 有没有什么捷径可以让我只重命名android studio中代码的某些部分?

Just select area that you want and then with command ctrl+R open window replace and checked In Selection then type what you want to replace and enjoy from that. 只需选择所需的区域,然后使用ctrl+R命令打开窗口替换并选中In Selection然后输入要替换的内容并从中享用。

喜欢这张图片

The easiest way is: 最简单的方法是:

Dog dog = new Dog();
dog.bark();

Dog dog1 = new Dog();
dog1.bark();

Dog dog2 = new Dog();
dog2.bark();

Dog dog3 = new Dog();
dog3.bark();

Manually as I have done, you can also do same, because there is no way to create variables as you want in sequence. 正如我所做的那样,您也可以手动进行操作, 因为无法按需顺序创建变量。

Right now you can copy/paste my code but if you want to do for more objects you can do manually because it will be better. 现在,您可以复制/粘贴我的代码,但是如果要处理更多对象,则可以手动执行,因为这样会更好。

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

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