SavingsAccount构造函数

时间:2014-09-30 13:55:17

标签: java constructor

如果我有类名SavesAccount,则有两个实例变量,例如private double balance和private double 感兴趣的是如何编写具有initialBalance和兴趣的构造函数?

2 个答案:

答案 0 :(得分:1)

public class SavingsAccount {

private double balance;
private double interest;

public SavingsAccount(double balance, double interest) {
this.balance = balance;
this.interest = interest;
}
}

答案 1 :(得分:0)

喜欢这个吗?

public SavingsAccount(double balance, double interest) {
    this.balance = balance;
    this.interest = interest;
}

public SavingsAccount() {
    this(0d, 1d); // default is 0 for balance, 1 for interest rate
}