【Java 实习】Day 02
作者:互联网
实例:银行
案例:银行卡账户类方法
存钱取钱转账方法模拟场景:张三使用建设银行卡存入了3000
李四使用农业银行卡存入5000
王五使用交通银行卡存入了4000
李四转账2000给张三
张三转账1000给王五体现出继承关系和多态
- 定义一个抽象类Bank作为所有银行类的父类
- 定义三个银行类继承Bank
- Bank中定义存钱、取钱和转账的方法
- 由于Bank无法获取具体银行名称,因此在三个银行子类中需要重写存钱和取钱方法
- 定义测试类BankTest
- 用到的快捷键
快捷键 | 说明 |
---|---|
Alt+Insert | 快速定义构造函数、set函数和get函数 |
Alt+Enter | 补全变量定义 |
\** +Enter | 注释 |
//Bank.java
package pers.syx.beans;
public abstract class Bank {
private String bankName; //银行名
private double accountBalance; //账户余额
private String accountName; //账户名
/**
* 存钱方法
* @param money
* @param accountName
*/
public void saveMoney(double money, String accountName){
this.setAccountName(accountName);
double accountBalance = this.getAccountBalance();
accountBalance += money;
this.setAccountBalance(accountBalance);
}
/**
* 取钱方法
* @param money
* @param accountName
*/
public void takeMoney(double money, String accountName){
this.setAccountName(accountName);
double accountBalance = this.getAccountBalance();
accountBalance -= money;
this.setAccountBalance(accountBalance);
}
/**
* 转账方法:将钱从 bank1 转到 bank2
* @param money
* @param bank1
* @param bank2
*/
public void transferMoney(double money, Bank bank1, Bank bank2){
double accountBalance = bank1.getAccountBalance();
accountBalance -= money;
bank1.setAccountBalance(accountBalance);
accountBalance = bank2.getAccountBalance();
accountBalance += money;
bank2.setAccountBalance(accountBalance);
System.out.println(bank1.getAccountName()+"转账"+money+"给"+bank2.getAccountName());
}
public Bank(String bankName, double accountBalance, String accountName) {
this.bankName = bankName;
this.accountBalance = accountBalance;
this.accountName = accountName;
}
public Bank() {
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public double getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(double accountBalance) {
this.accountBalance = accountBalance;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
}
//CCB.java
package pers.syx.beans;
public class CCB extends Bank{
public static final String BANK_NAME = "建设银行";
/**
* 重写父类存钱方法
* @param money
* @param accountName
*/
@Override
public void saveMoney(double money, String accountName) {
super.saveMoney(money, accountName);
this.setBankName(BANK_NAME);
System.out.println(this.getAccountName()+"在"+this.getBankName()+"中存入了"+money);
}
/**
* 重写父类取钱方法
* @param money
* @param accountName
*/
@Override
public void takeMoney(double money, String accountName) {
super.takeMoney(money, accountName);
this.setBankName(BANK_NAME);
System.out.println(this.getAccountName()+"从"+this.getBankName()+"中取出了"+money);
}
}
//ABC.java
package pers.syx.beans;
public class ABC extends Bank{
public static final String BANK_NAME = "农业银行";
/**
* 重写父类存钱方法
* @param money
* @param accountName
*/
@Override
public void saveMoney(double money, String accountName) {
super.saveMoney(money, accountName);
this.setBankName(BANK_NAME);
System.out.println(this.getAccountName()+"在"+this.getBankName()+"中存入了"+money);
}
/**
* 重写父类取钱方法
* @param money
* @param accountName
*/
@Override
public void takeMoney(double money, String accountName) {
super.takeMoney(money, accountName);
this.setBankName(BANK_NAME);
System.out.println(this.getAccountName()+"从"+this.getBankName()+"中取出了"+money);
}
}
//BC.java
package pers.syx.beans;
public class BC extends Bank{
public static final String BANK_NAME = "交通银行";
/**
* 重写父类存钱方法
* @param money
* @param accountName
*/
@Override
public void saveMoney(double money, String accountName) {
super.saveMoney(money, accountName);
this.setBankName(BANK_NAME);
System.out.println(this.getAccountName()+"在"+this.getBankName()+"中存入了"+money);
}
/**
* 重写父类取钱方法
* @param money
* @param accountName
*/
@Override
public void takeMoney(double money, String accountName) {
super.takeMoney(money, accountName);
this.setBankName(BANK_NAME);
System.out.println(this.getAccountName()+"从"+this.getBankName()+"中取出了"+money);
}
}
//BankTest.java
package pers.syx.beans;
public class BankTest {
public static void main(String[] args) {
Bank bank1 = new CCB();
bank1.saveMoney(3000,"张三");
Bank bank2 = new ABC();
bank2.saveMoney(5000,"李四");
Bank bank3 =new BC();
bank3.saveMoney(4000,"王五");
bank2.transferMoney(2000,bank2,bank1);
bank1.transferMoney(1000,bank1,bank3);
}
}
- 抽象类不能实例化对象,即抽象类不可以有具体的实例
- 抽象方法没有方法体,且只能存在于抽象类中。没有方法体的方法一定为抽象方法
- 抽象类中可以有普通方法
- 抽象类只能被继承,子类必须重写父类中的抽象方法
- 一个类没有抽象方法,也可以定义为抽象类,但依然不能实例化对象。这个抽象类作为顶层父类,体现继承关系,但是定义为抽象类意义不大
- private和abstract冲突,final和abstract冲突,因为private和final类不能被继承
标签:02,Java,String,money,param,Day,accountBalance,accountName,public 来源: https://blog.csdn.net/qq_44714521/article/details/111996019