9.19 third day 注意事项

来源:互联网 发布:大数据的意义包括 多选 编辑:程序博客网 时间:2024/06/14 00:03

在对于继承和多态的理解


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

0 0
原创粉丝点击