练习目标:继承、多态、方法的重写。

练习目标:继承、多态、方法的重写。 在本练习中,将在银行项目中创建Account的两个子类:SavingAccount 和 CheckingAccount。

创建 Account类的两个子类:SavingAccount(存款账户) 和 CheckingAccount(透支账户)子类

1.修改Account类;将balance属性的访问方式改为protected
2.创建 SavingAccount 类,该类继承Account类
3.该类必须包含一个类型为double的interestRate(利率)属性
4.该类必须包括带有两个参数(balance和interest_rate)的共有构造器。该构造器必须通过调用super(balance)将balance参数传递给父类构造器。

实现CheckingAccount类
1.CheckingAccount类必须扩展Account类
2.该类必须包含一个类型为double的overdraftProtection(透支额度)属性。
3.该类必须包含一个带有参数(balance)的共有构造器。该构造器必须通过调用super(balance)将balance参数传递给父类构造器。
4.给类必须包括另一个带有两个参数(balance 和 protect)的共有构造器。该构造器必须通过调用super(balance)并设置overdragtProtection属性,将balance参数传递给父类构造器。
5.CheckingAccount类必须覆盖withdraw方法。此方法必须执行下列检查。如果当前余额足够弥补取款amount,则正常进行。如果不够弥补但是存在透支保护,则尝试用overdraftProtection得值来弥补该差值(balance-amount).如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。

6.在主exercise1目录中,编译并执行TestBanking程序。输出应为:
Creating the customer Jane Smith.
Creating her Savings Account with a 500.00 balance and 3% interest.
Creating the customer Owen Bryant.
Creating his Checking Account with a 500.00 balance and no overdraft protection.

Creating the customer Tim Soley.
Creating his Checking Account with a 500.00 balance and 500.00 in overdraft prot
ection.
Creating the customer Maria Soley.
Maria shares her Checking Account with her husband Tim.

Retrieving the customer Jane Smith with her savings account.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
Withdraw 400.00: false
Customer [Simms, Jane] has a balance of 324.88

Retrieving the customer Owen Bryant with his checking account with no overdraft
protection.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
Withdraw 400.00: false
Customer [Bryant, Owen] has a balance of 324.88

Retrieving the customer Tim Soley with his checking account that has overdraft p
rotection.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
Withdraw 400.00: true
Customer [Soley, Tim] has a balance of 0.0

Retrieving the customer Maria Soley with her joint checking account with husband
Tim.
Deposit 150.00: true
Withdraw 750.00: false
Customer [Soley, Maria] has a balance of 150.0

///SavingAccount
package banking;

public class SavingAccount extends Account {
private double interestRate;

public SavingAccount(double b) {
super(b);
}

public double getInterestRate() {
return interestRate;
}

public SavingAccount(double b, double i) {
super(b);
this.interestRate = i;
}
}

///CheckingAccount

package banking;

public class CheckingAccount extends Account {
public double getOverdraftProtection() {
return overdraftProtection;
}

double overdraftProtection;

public CheckingAccount(double b) {
super(b);
}

public CheckingAccount(double b, double protect) {
super(b);
this.overdraftProtection = protect;
}

public boolean withdraw(double i)
{
if(balance>=i)
{
balance-=i;
System.out.print("Withdraw "+i);
return true;
}
else if(balance+overdraftProtection>=i)
{
balance=0;
System.out.print("Withdraw "+i);
return true;
}
else
{
System.out.print("Withdraw "+i);
return false;
}
}
}

///Testbanking 类

public class TestBanking {
private static void d(Bank bk1)
{
System.out.println("create the customer "+bk1.getCustomer(bk1.getNumOfCustomers()).getFirstName()+" "+bk1.getCustomer(bk1.getNumOfCustomers()).getLastName()+".");
}

public static void main(String[] args) {

Bank bk1=new Bank();
bk1.addCustomer( "Jane" , "Smith" );
d(bk1);
SavingAccount a1=new SavingAccount(500,3);
System.out.println("Creating her Savings Account with a "+a1.getBalance()+" balance "+"and"+a1.getInterestRate()+"% "+"interest.");
bk1.addCustomer("Owen","Bryant");
d(bk1);
CheckingAccount a2=new CheckingAccount (500,0);
System.out.println("Creating her Checking Account with a "+a2.getBalance()+" balance "+"and "+a2.getOverdraftProtection()+" overdraftprotection.");
System.out.println();
bk1.addCustomer("Tim","Soley");
d(bk1);
CheckingAccount a3=new CheckingAccount (500,500);
System.out.println("Creating his Checking Account with a "+a3.getBalance()+" balance "+"and "+a3.getOverdraftProtection()+" overdraftprotection.");
bk1.addCustomer("Marry","Soley");
d(bk1);
System.out.println("Maria shares her Checking Account with her husband Tim.");
System.out.println();
System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
System.out.println(":"+a1.withdraw(150));
System.out.println(":"+a1.deposit(22.50));
System.out.println(":"+a1.withdraw(47.62));
System.out.println(":"+a1.withdraw(400));
System.out.println("Customer ["+bk1.getCustomer(1).getFirstName()+","+bk1.getCustomer(1).getLastName()+"] has a balance of "+a1.getBalance());
System.out.println();

System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
System.out.println(":"+a2.withdraw(150));
System.out.println(":"+a2.deposit(22.50));
System.out.println(":"+a2.withdraw(47.62));
System.out.println(":"+a1.withdraw(400));
System.out.println("Customer ["+bk1.getCustomer(2).getFirstName()+","+bk1.getCustomer(2).getLastName()+"] has a balance of "+a2.getBalance());
System.out.println();

System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
System.out.println(":"+a3.withdraw(150));
System.out.println(":"+a3.deposit(22.50));
System.out.println(":"+a3.withdraw(47.62));
System.out.println(":"+a3.withdraw(400));
System.out.println("Customer ["+bk1.getCustomer(3).getFirstName()+","+bk1.getCustomer(3).getLastName()+"] has a balance of "+a3.getBalance());
System.out.println();

System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
System.out.println(":"+a3.deposit(150));
System.out.println(":"+a3.withdraw(750));
System.out.println("Customer ["+bk1.getCustomer(4).getFirstName()+","+bk1.getCustomer(4).getLastName()+"] has a balance of "+a3.getBalance());

}
}
///运行结果
create the customer Jane Smith.
Creating her Savings Account with a 500.0 balance and3.0% interest.
create the customer Owen Bryant.
Creating her Checking Account with a 500.0 balance and 0.0 overdraftprotection.

create the customer Tim Soley.
Creating his Checking Account with a 500.0 balance and 500.0 overdraftprotection.
create the customer Marry Soley.
Maria shares her Checking Account with her husband Tim.

Retrieving the customer Owen Bryant with his checking account with no overdraft protection.
Withdraw 150.0:true
Deposit 22.5:true
Withdraw 47.62:true
Withdraw 400.0:false
Customer [Jane,Smith] has a balance of 324.88

Retrieving the customer Owen Bryant with his checking account with no overdraft protection.
Withdraw 150.0:true
Deposit 22.5:true
Withdraw 47.62:true
Withdraw 400.0:false
Customer [Owen,Bryant] has a balance of 324.88

Retrieving the customer Tim Soley with his checking account that has overdraft protection.
Withdraw 150.0:true
Deposit 22.5:true
Withdraw 47.62:true
Withdraw 400.0:true
Customer [Tim,Soley] has a balance of 0.0

Retrieving the customer Maria Soley with her joint checking account with husband Tim.
Deposit 150.0:true
Withdraw 750.0:false
Customer [Marry,Soley] has a balance of 150.0

时间: 2024-10-14 02:13:58

练习目标:继承、多态、方法的重写。的相关文章

OC 类的继承、方法的重写和重载

一.类的继承 Objective-c中类的继承与C++类似,不同的是Objective-c不支持多重继承,一个类只能有一个父类,单继承使Objective-c的继承关系很简单,易于管理程序.Objective-c中所有类的父类是NSObject. Objective-c中类的继承例子: @interface Person: NSObject { NSString* name; int age; } - (NSString*) name; - (int) age; - (void) setName

java实现多态 方法的重写和重载的区别

1.方法的重写 思路:先写一个父类People,在类中定义一个 print方法 ,然后写一个子类 Student 继承父类,重写print方法. //父类 class People{ public void print(){ System.out.println("这是父类的print方法!"); } } //子类 继承父类 class Student extends People{ public void print(){ System.out.println("这是子类的

(1) 深入理解Java面向对象三大特性 封装 继承 多态

转眼已经工作快6年了,最开始做了2年J2EE:然后整了2年的数据仓库,主要是Cognos的报表开发:现在从事4G LTE核心网的开发,用的语言任然是Java,但写代码的机会不多,基本都是看代码找BUG,偶尔做点new feature也是在比较成熟的框架上复制.粘贴.修改,大部分时间还是在理解业务,钱多.事少.离家近,当时来这家公司图的是后面2点,2年过去了,英文水平有所提升,对敏捷开放也有一定的了解,但技术方面明显退步了或者说没有进步吧,本来以前也不怎么好,因为工作上用不到,自己也没怎么学习,所

Java面向对象三大特性 封装 继承 多态

1.封装 封装的定义: 首先是抽象,把事物抽象成一个类,其次才是封装,将事物拥有的属性和动作隐藏起来,只保留特定的方法与外界联系 为什么需要封装: 封装符合面向对象设计原则的第一条:单一性原则,一个类把自己该做的事情封装起来,而不是暴露给其他类去处理,当内部的逻辑发生变化时,外部调用不用因此而修改,他们只调用开放的接口,而不用去关心内部的实现 举例: public class Human { private int age; private String name; public int get

java 子类继承父类成员变量的隐藏、实现方法的重写

成员变量的隐藏和方法的重写 Goods.java public class Goods { public double weight; public void oldSetWeight(double w) { weight=w; System.out.println("double型的weight="+weight); } public double oldGetPrice() { double price = weight*10; return price; } } CheapGoo

java基础疑难点总结之成员变量的继承,方法重载与重写的区别,多态与动态绑定

1.成员变量的继承 1.1要点 子类用extends关键字继承父类.子类中可以提供新的方法覆盖父类中的方法.子类中的方法不能直接访问父类中的私有域,子类可以用super关键字调用父类中的方法.在子类中可以增加域,增加方法或者覆盖超类的方法,然而绝对不能删除继承的任何域和方法. 在一个子类被创建的时候,首先会在内存中创建一个父类对象,然后在父类对象外部放上子类独有的属性,两者合起来形成一个子类的对象.所以所谓的继承使子类拥有父类所有的属性和方法其实可以这样理解,子类对象确实拥有父类对象中所有的属性

java学习中,面向对象的三大特性:封装、继承、多态 以及 super关键字和方法的重写(java 学习中的小记录)

java学习中,面向对象的三大特性:封装.继承.多态 以及 super关键字和方法的重写(java 学习中的小记录) 作者:王可利(Star·星星) 封装     权限修饰符:public 公共的,private 私有的     封装的步骤:          1.使用private 修饰需要封装的成员变量.          2.提供一个公开的方法设置或者访问私有的属性              设置 通过set方法,命名格式:     set属性名();  属性的首字母要大写 访问 通过ge

oc语言学习之基础知识点介绍(四):方法的重写、多态以及self、super的介绍

一.方法重写 /* 重写:当子类继承了父类的方法时,如果觉得父类的方法不适合,那么可以对这个方法进行重新实现,那么这个就重写. 注意:也就是说,一定只能发生在父类和子类关系中. 然后是子类重新实现父类的方法,绝对不是再写一个自己类的方法. 代码中原话叫:子类重写父类方法. 因为父类定义的方法不一定适用于子类. 注意:如果有重写,那么调用的是自己重写后的方法,如果没有重写,那么就调用的是父类的方法. 所以我们方法有一个执行的过程: 1.先去自己类里面找这个方法,如果找到就执行. 2.如果没找到,就

多态,虚拟方法,重写,接口,类库,委托,is,as运算符,泛型集合,万能变量

多态:简而言之就是龙生九子,各有不同 有了继承,才有了多态 1.虚方法 virtual重写 override 父类中的方法,在子类中并不适用,那么子类需要自主更改继承的方法或者是属性,那父类中加了virtual关键字的方法才可以被子类重写 子类重写父类的方法使用的是override关键字 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 多态 { publ