OOP实验5-继承

实验五 继承

实验五 继承
本实验4学时

  1. 实验目的:
    利用继承,实现程序功能的扩充

  2. 实验环境与条件:
    JDK, Netbeans

  3. 实验内容:
    在前一个实验的基础上, 利用类的继承, 扩充一个新的功能.
    程序具体的功能/作用, 自己设定.

内容参考示例:
在前一个实验的基础上完成。在已有的Account类基础上, 扩充出一个具有转账功能的新的类CCBAccount.
文件Account.Java

public class Account {
    private String accountID;
    private int balance;
    public void deposit(int amount) {
        this.balance += amount;
    }
    public void withdraw(int amount) {
        this.balance -= amount;
    }
    public Account(String accountID, int balance) {
        this.accountID = accountID;
        this.balance = balance;
    }
    public String getAccountID() {
        return accountID;
    }
    public int getBalance() {
        return balance;
    }
}

文件CCBAccount.Java

public class CCBAccount extends Account {
    public CCBAccount(String accountID, int balance) {
        super(accountID, balance);
    }
    public void transfer(Account to, int amount) {
        this.withdraw(amount);
        to.deposit(amount);
    }
}

文件MainClass.Java

public class MainClass {
    public static void main(String[] args) {
        CCBAccount account1 = new CCBAccount("6200001", 1000);
        account1.deposit(100);
        System.out.println(account1.getAccountID() + " : " + account1.getBalance());
        CCBAccount account2 = new CCBAccount("6200002", 1000);
        account2.withdraw(100);
        System.out.println(account2.getAccountID() + " : " + account2.getBalance());
        account1.transfer(account2, 100);
        System.out.println(account1.getAccountID() + " : " + account1.getBalance());
        System.out.println(account2.getAccountID() + " : " + account2.getBalance());
    }
}

你可能感兴趣的:(OOP)