我有两个用于创建BankAccount的构造函数
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);
}
(不要重复自己)原则。我写了这个,但它引起了错误......请帮助......
select * from tablename
where id like :bindVariable
答案 0 :(得分:1)
使用this
public BankAccount(int accountNumber) {
this(accountNumber,0.0);
}
答案 1 :(得分:1)
检查以下代码:
public class BankAccount {
int accountNumber;
double balance;
public BankAccount(int accountNumber) {
// TODO Auto-generated constructor stub
this(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);
}
}
编译好。您将this
与构造函数一起使用,因为它们不是方法。