【内部类 外部类】
这次练习出现很多错误,暴露出代码不熟练的问题
其中 有个严重的错误,把外部类写到了内部类 main 函数与Account类平行
调用时出现静态调用的问题
package chapter08_编程练习题;
import java.util.Date;
public class Show07 {
public static void main(String[] args) {
Account account = new Account(1122, 20000);
Account.setAnnualInterestRate(4.5);
account.deposit(3000);
account.withDraw(2500);
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(){ 多打了一个括号
class Account {
private int id;
private double balance;
private static double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
dateCreated = new Date();
}
/*
* Account(int id,double balance){ this.balance = balance; this.id = id; }
*/
// 改成: 增加public
public Account(int id, double balance) {
this.balance = balance;
this.id = id;
dateCreated = new Date();
}
public int getid() {
return id;
}
public double getbalance() {
return balance;
}
public static double getAnnualInterestRate() {// 获取年利率
return annualInterestRate;
}
public void setid(int newid) {
id = newid;
}
public void setbalance(double newbalance) {// 设置余额
balance = newbalance;
}
/*
* public static void setannualInterestRate(double annualInterestRate){
* this.annualInterestRate = annualInterestRate;//annualInterestRate是静态的
* 所以不能用this
* }
*/
// 改成:
public static void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public double getMonthlyInterest() {// 月利率
return balance * (annualInterestRate / 1200);
}
public java.util.Date getdateCreated() {// 获取余额
return dateCreated;
}
public void withDraw(double withDraw) {// 取钱
balance -= withDraw;
}
public void deposit(double deposit) {// 存钱
balance += deposit;
}
}