简体   繁体   English

使用Intellij在Java中重构构造函数

[英]Refactor a constructor in Java with Intellij

I'm refactoring my java code with IntelliJ and need to add an extra parameter on a constructor. 我用IntelliJ重构我的java代码,需要在构造函数上添加一个额外的参数。

Let's say I'm editing the class SocialNetwork and just changed how I add a friend, instead of just a name now the class also has an Integer that is the age. 假设我正在编辑SocialNetwork类,只是改变了我添加朋友的方式,而不仅仅是一个名字,现在该类也有一个年龄的整数。

Of course IntelliJ will tell me the constructor doesn't exist. 当然IntelliJ会告诉我构造函数不存在。 So far so good. 到现在为止还挺好。

Now the problem, on Eclipse it is possible to choose among create a new constructor or change the existing one. 现在问题是,在Eclipse上可以选择创建新构造函数或更改现有构造函数。 When I hit Alt + Enter on IntelliJ I only get the option to create a new constructor. 当我在IntelliJ上Alt + Enter ,我只能选择创建一个新的构造函数。

Is there a way for IntelliJ for offer the option do add an Integer parameter to the existing constructor? 是否有一种方法可以让IntelliJ提供选项,为现有的构造函数添加一个Integer参数?

在此输入图像描述

在此输入图像描述

Intellij treats constructors just like regular methods, so the same refactoring procedures apply to constructors (save for a few caveats, like editing the name) Intellij就像常规方法一样处理构造函数,因此相同的重构过程适用于构造函数(除了一些注意事项,比如编辑名称)

You can use Refactor -> Change Signature... to add and remove parameters, by accessing either from the definition, or any statement that uses new 您可以使用Refactor -> Change Signature...来添加和删除参数,方法是从定义或任何使用new语句中访问

在此输入图像描述

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

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