向现有构造函数添加字段的快捷方式 [英] Shortcut for adding fields to existing constructor

查看:156
本文介绍了向现有构造函数添加字段的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



示例:是否有任何快捷方式允许我将添加一个字段到现有构造函数的参数列表?



我讨厌这个课程:

  public class A {
int a;
int b;

public A(int a,int b){
this.a = a;
this.b = b;
}
}

当我添加一个字段 int c (或许多字段)我想要添加到构造函数的参数列表中,并将参数分配给字段:

  public class A {
int a;
int b;
int c; //这是新的

public A(int a,int b,int c){
this.a = a;
this.b = b;
this.c = c;
}
}

我现在手动创建参数,然后按 CTRL + 1 ,然后选择将参数分配给字段



但如果我添加多个字段一旦这不是一个很好的解决方案imho。



我不想创建一个新的构造函数!

解决方案

我将使用更改方法签名重构(Mac上的选项+命令+ c)将额外的参数添加到构造函数。这样调用构造函数的现有代码可以将合理的默认值作为参数传递(如果您愿意)。然后选择多次CTRL + 1可以按照您的建议将新字段快速修复到课堂。


Is there any shortcut that allows me to add a field to the argument list of an existing constructor?

Example:

I hava this class:

public class A {
    int a;
    int b;

    public A(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

when i add a field int c (or many fields) i want to add it to the argumentlist of the constructor and assign the parameter to the field:

public class A {
    int a;
    int b;
    int c; //this is new

    public A(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

i currently do this by creating the parameter manually and then press CTRL + 1and then choose "assign parameter to field"

but if i add more than one field at once this isn't really a good solution imho.

I don't want to create a new constructor!

解决方案

I would use the "change method signature" refactoring first (option+command+c on mac) to add the extra parameter(s) to the constructor. This way existing code that calls the constructor can pass sensible default as parameters (if you like). Then choose as many times CTRL+1 to quick fix the new fields into the class as you suggested.

这篇关于向现有构造函数添加字段的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆