gpt4 book ai didi

java - Java 中的简单重构(基础)

转载 作者:行者123 更新时间:2023-11-30 08:52:39 25 4
gpt4 key购买 nike

我有这两个用于创建 BankAccount 的构造函数

    public BankAccount(int accountNumber , double balance) {
// TODO Auto-generated constructor stub
this.accountNumber = accountNumber;
this.balance = balance;
System.out.println(this.accountNumber+" "+this.balance);
}

public BankAccount(int accountNumber) {
// TODO Auto-generated constructor stub
this.accountNumber = accountNumber;
this.balance = 0;

System.out.println(this.accountNumber+" "+this.balance);
}

我想重构它并符合DRY(不要重复自己)原则。我写了这个但是它引发了一个错误...请帮助...

public BankAccount(int accountNumber) {
// TODO Auto-generated constructor stub
BankAccount(accountNumber,0.0);
}
public BankAccount(int accountNumber , double balance) {
// TODO Auto-generated constructor stub
this.accountNumber = accountNumber;
this.balance = balance;

System.out.println(this.accountNumber+" "+this.balance);

}

最佳答案

使用这个

public BankAccount(int accountNumber) {
this(accountNumber,0.0);
}

关于java - Java 中的简单重构(基础),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30115606/

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