继承性与 super 的练习使用
一
代码一
package eightdemo;
/**
* 实验 类的继承,super
* 1、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;
* 包含的方法:访问器方法(getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方法withdraw(),存款方法deposit()。
*/
public class Account {
protected int id;//账户
protected double balance;//余额
protected double annualInterestRate;//年利率
public Account(int id, double balance, double annualInterestRate){
super();
this.id = id;
this.balance = balance;
this.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 this.annualInterestRate / 12;
}
//存钱
public void deposit (double amount){
balance += amount;
}
//取钱
public void withdraw (double amount){
if(this.balance >= amount)
balance -= amount;
else
System.out.println("余额不足");
}
}
代码二
package eightdemo;
/**
* 写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%的Account对象。使用withdraw方法提款30000元,并打印余额。
* 再使用withdraw方法提款2500元,使用deposit方法存款3000元,然后打印余额和月利率。
*/
public class TestAccount {
public static void main(String[] args) {
Account acct = new Account(1122, 20000, 0.045);
acct.withdraw(30000);
System.out.println("当前余额为:" + acct.getBalance());
acct.withdraw(2500);
acct.deposit(3000);
System.out.println("当前余额为:" + acct.getBalance());
System.out.println("月利率为:" + acct.getMonthlyInterest());
}
}
二
代码一
package eightdemo;
/**
* 实验 类的继承,super
* 1、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;
* 包含的方法:访问器方法(getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方法withdraw(),存款方法deposit()。
*/
public class Account {
protected int id;//账户
protected double balance;//余额
protected double annualInterestRate;//年利率
public Account(int id, double balance, double annualInterestRate){
super();
this.id = id;
this.balance = balance;
this.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 this.annualInterestRate / 12;
}
//存钱
public void deposit (double amount){
balance += amount;
}
//取钱
public void withdraw (double amount){
if(this.balance >= amount)
balance -= amount;
else
System.out.println("余额不足");
}
}
代码二
package eightdemo;
/**
* 2、创建Account类的一个子类CheckAccount代表可透支的账户,该账户中定义一个属性overdraft代表可透支限额。
* 在CheckAccount类中重写withdraw方法,其算法如下:
* 如果(取款金额<账户余额),
* 可直接取款
* 如果(取款金额>账户余额),
* 计算需要透支的额度
* 判断可透支额overdraft是否足够支付本次透支需要,如果可以
* 将账户余额修改为0,冲减可透支金额
* 如果不可以
* 提示用户超过可透支额的限额
*
*
*/
public class CheckAccount extends Account{
private double overdraft;//可透支额度
public double getOverdraft(){
return overdraft;
}
public void setOverdraft(double overdraft){
this.overdraft = overdraft;
}
public CheckAccount(int id, double init_balance, double annualIterestRate , double overdraft){
super(id, init_balance, annualIterestRate);
this.overdraft = overdraft;
}
//存在透支额度的取款操作
public void withdraw(double amount){
if(balance >= amount){
balance -= amount;
}else if(overdraft >= amount - balance){
overdraft -=(amount - balance);
balance = 0;
}else {
System.out.println("超过可透支额的限额");
}
}
}
代码三
package eightdemo;
/**
* 要求:写一个用户程序测试CheckAccount类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%,
* * 可透支限额为5000元的CheckAccount对象。
* * 使用withdraw方法提款5000元,并打印账户余额和可透支额。
* * 再使用withdraw方法提款18000元,并打印账户余额和可透支额。
* * 再使用withdraw方法提款3000元,并打印账户余额和可透支额。
* *
* * 提示:
* * (1) 子类CheckAccount的构造方法需要将从父类继承的3个属性和子类自己的属性全部初始化。
* * (2) 父类Account的属性balance被设置为private,但在子类CheckAccount的withdraw方法中需要修改它的值,
* * 因此应修改父类的balance属性,定义其为protected。
*/
public class TestCheckAccount {
public static void main(String[] args) {
CheckAccount ca = new CheckAccount(1122, 20000, 0.045, 5000);
ca.withdraw(5000);
System.out.println("当前余额为:" + ca.getBalance());
System.out.println("当前可透支额度为:" + ca.getOverdraft());
ca.withdraw(18000);
System.out.println("当前余额为:" + ca.getBalance());
System.out.println("当前可透支额度为:" + ca.getOverdraft());
ca.withdraw(3000);
System.out.println("当前余额为:" + ca.getBalance());
System.out.println("当前可透支额度为:" + ca.getOverdraft());
}
}
韧桂 2019-12-25