Java语言程序设计【基础篇】【chapter08_8.7】

【内部类 外部类】


这次练习出现很多错误,暴露出代码不熟练的问题

其中 有个严重的错误,把外部类写到了内部类 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;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值