编程语言
首页 > 编程语言> > 【Java 实习】Day 02

【Java 实习】Day 02

作者:互联网

实例:银行

案例:银行卡账户类方法
存钱取钱转账方法模拟场景:

张三使用建设银行卡存入了3000
李四使用农业银行卡存入5000
王五使用交通银行卡存入了4000
李四转账2000给张三
张三转账1000给王五

体现出继承关系和多态

  1. 定义一个抽象类Bank作为所有银行类的父类
  2. 定义三个银行类继承Bank
  3. Bank中定义存钱、取钱和转账的方法
  4. 由于Bank无法获取具体银行名称,因此在三个银行子类中需要重写存钱和取钱方法
  5. 定义测试类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);
    }
}

抽象类的简单介绍

标签:02,Java,String,money,param,Day,accountBalance,accountName,public
来源: https://blog.csdn.net/qq_44714521/article/details/111996019