您的位置:首页 > 其它

9.19 third day 注意事项

2015-09-20 15:11 148 查看
在对于继承和多态的理解

package third;

public class Test1 {
public static void main(String[] args) {

// Account a = new Account(1122,20000,0.045);

// a.withdraw(30000);

// a.deposit(2500);
CheckAccount c = new CheckAccount(1122, 20000, 0.045, 5000);
c.withdraw(5000);
c.withdraw(18000);
c.deposit(2000);
}

}

class Account{
private int id;
private double balance;
private double annualInterestRate;

public Account (int id, double balance, double annualInterestRate )
{
this.id = id;this.balance = balance;this.annualInterestRate = annualInterestRate;

}

@Override
public String toString() {
return "Account [id=" + id + ", balance=" + balance
+ ", annualInterestRate=" + annualInterestRate + "]";
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public double getAnnualInterestRate() {
return annualInterestRate;
}

public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}

public double getMonthlyInterest(){
return (annualInterestRate/12);
}
public void withdraw (double amount){
if(balance > amount)
{
balance = balance - amount;
System.out.println("your account money:"+balance);
}
else
System.out.println("money not enough");
}
public void deposit (double amount){
balance +=amount;
System.out.println("your account money:"+balance);
System.out.println(getMonthlyInterest());
}

}

class CheckAccount extends Account{

public CheckAccount(int id, double balance, double annualInterestRate, double overDraft) {   //在子类继承父类,必须重写父类的构造函数
super(id, balance, annualInterestRate);   //super在子类中可以调用父类的构造器。
// TODO Auto-generated constructor stub
this.overDraft = overDraft;
}
private double overDraft;
@Override
public void withdraw(double amount) {
// TODO Auto-generated method stub
if(getBalance()>amount){
super.withdraw(amount);
System.out.println("your can overdraft money:"+ overDraft);
}
else
if((getBalance()+overDraft)>amount){

overDraft = overDraft - amount + getBalance();
setBalance(0);
System.out.println("your account money:"+getBalance());
System.out.println("your can overdraft money:"+ overDraft);
}
else
{
System.out.println("Don't have enough money");
}

}

}

多态(自己初步的偶遇)

在声明函数和调用函数的时候能用上,在函数声明的时候

A a = new B;

其中B是A的子类

在函数调用的时候  toString(A a)

其中的参数a可以是 类B
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: