[英]resolving bracketing error when creating constructors in Java
I am getting a bracketing error in Eclipse (lines 15 & 18) "public Account myCustomAccount ... balance = initial balance; }" when I try to open my second constructor in the following program. 当我尝试在以下程序中打开第二个构造函数时,在Eclipse中出现括弧错误(第15和18行)“ public Account myCustomAccount ... balance = initial balance;}”。 The program is for Dietel "Introduction to Programming" chapter 9 exercise 7. 该程序适用于Dietel“编程入门”第9章练习7。
I suspect that I am creating the constructor incorrectly. 我怀疑自己在错误地创建了构造函数。 What advice do you offer? 您提供什么建议? (Thank you kindly in advance!!) (提前谢谢您!!)
import java.util.Date;
public class Account {
//declare required variables
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0; //assume all accounts have the same interest rate
private Date dateCreated = new Date(); //no-argument instance stores the present date
//define default & custom constructors
public Account mydefaultaccount = new Account(); //no-argument instance of Account
public Account myCustomAccount = new Account(int identNum, double initialBalance) {
id = identNum;
balance = initialBalance;
}
//define getters
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double annualInterestRate() {
return annualInterestRate;
}
public Date getDate() {
return dateCreated;
}
//define setters
public void setId(int idSetter) {
id = idSetter;
}
public void setBalance(double balanceSetter) {
balance = balanceSetter;
}
public void setAnnualInterestRate(double annualSetter) {
annualInterestRate = annualSetter;
}
//define required monthly interest rate getter
public double getMonthlyInterestRate() {
double moInt = annualInterestRate / 12;
return moInt;
}
//define modifiers
public double withdraw(int withdraw) {
balance = balance - withdraw;
}
public double deposit(int deposit) {
balance = balance + deposit;
}
}
That isn't how you define constructors. 那不是定义构造函数的方式。 Constructors should follow the form: 构造函数应遵循以下形式:
public className(parameters) {}
Then, to instantiate the class, call this: 然后,要实例化该类,请调用:
ClassName variable = new ClassName(Parameters);
In your case, 就你而言
public Account() {
/* Body */
}
public Account(int identNum, double initialBalance) {
/* Body */
}
And to instantiate, 并实例化
Account ac = new Account(Parameters);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.