gpt4 book ai didi

java - Eclipse 生成复制构造函数

转载 作者:行者123 更新时间:2023-12-01 06:37:26 26 4
gpt4 key购买 nike

如何在eclipse中生成复制构造函数?

我知道生成构造函数的方法(使用字段或来自 super )。

例如:

public class HeroInfo {
private int hp;
private float power;

public HeroInfo() { // no-arg constructor
super();
}

public HeroInfo(HeroInfo info){ // This is copy constructor
super();
this.hp = info.hp;
this.power = info.power
}

...
}

我的对象包含很多变量,所以写起来很长。

那么 eclipse 有一个热键或者它的方法吗?

最佳答案

Eclipse 中没有为复制构造函数生成源代码的选项,但您可以使用此策略简化该过程。

  1. 首先按以下键为所有字段生成构造函数:Shift + Alt + S, O.

  2. 然后在构造函数中选择字段分配,并按 Ctrl + F 打开查找和替换对话框。

  3. =[space] 替换为 =[space][Identifier]。
  4. 将构造函数签名中的参数替换为[类型] [标识符]

例如,假设我生成的构造函数是:

    public Record(String name, String organization, String systemName) {
this.name = name;
this.organization = organization;
this.systemName = systemName;
}

我对所选文本执行查找和替换,将 = 替换为 = record。 结果:

    public Record(String name, String organization, String systemName) {
this.name = record.name;
this.organization = record.organization;
this.systemName = record.systemName;
}

然后将签名中的参数替换为标识符名称:

    public Record(Record record) {
this.name = record.name;
this.organization = record.organization;
this.systemName = record.systemName;
}

关于java - Eclipse 生成复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49467730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com