我正在制定一个程序,将银行账户作为对象.这些帐户具有利率,余额,ID和日期创建数据.根据我的理解,默认余额,ID和兴趣是0.默认情况下,利率未定义.我正在学习的那本书表明,使用"Circle(){}"完成了一个无参数的构造函数.我在帐户类中使用了"account(){}".当我在jGRASP中运行程序时,我得到两个构造函数的错误"无效的方法声明;返回类型".它正在认识到我打算将构造函数作为方法.我需要了解什么才能让我的构造函数不被认为是方法?
在运行第一个构造函数时,我理解我们使用默认值创建一个名为account的Account对象.当我们运行第二个构造函数时,我们将帐户对象的值更改为指定的值
public class Bank{
public static void main(String[] args){
Account account = new Account(1122, 20000);
account.setAnnualInterestRate(4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " + account.getMonthlyInterest());
System.out.println("This account was created at " + account.getDateCreated());
}
}
class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private String dateCreated;
account(){
}
account(int newID, double newBalance){
id = newID;
balance = newBalance;
}
//accessor for ID
public int getID(){ …Run Code Online (Sandbox Code Playgroud)