Java学习第11天

2016-12-09  本文已影响0人  _Raye

继承:

继承的特性:

继承关键字:

先定义一个Person类:

package org.mobiletrain;
public class Person {
    //protected对于子类可以访问,但是对外界还是不公开
    protected String name;
    protected int age;
    
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    
    public String eat(){
        return String.format(name + "正在吃饭");
    }
    
    public String play(String gameName){
        return String.format(name + "正在玩" + gameName);
    }
}
package org.mobiletrain;
public class Student extends Person {   
    private String classCode;
    
    public Student(String name,int age){
        super(name,age);//用super调用父类构造器
    }
    
    public void setClassCode(String classCode){
        this.classCode = classCode;
    }
    
    public String study(){
        return String.format(classCode + "班的"+ age + "岁的" + name + "正在学习");
    }
}

测试之前的类:

package org.mobiletrain.test;

import org.mobiletrain.Person;
import org.mobiletrain.Student;
import org.mobiletrain.Teacher;

public class StudentTest {

    public static void main(String[] args) {
        //子类跟父类之间是一种is-a关系
        //把子类对象赋值给父类变量没有任何问题
        //stu1和teach都有person属性,但是在后续执行功能时需要强转型
        Person stu1 = new Student("二狗", 20);
        Person teacher = new Teacher("大锤", 30,"叫兽");
        
        //类型之间也可以通过强转
        ((Student) stu1).setClassCode("JavaEE");
        ((Teacher) teacher).teach("JAVA");
        
        //student中没有teach功能,强转后编译不报错,但是执行的时候会报错
        //((Teacher) stu1).teach("高等数学");
    }
}

面向对象实例:ATM机

先定义个人账户类:

package org.mobiletrain;

/**
 * 银行账户
 * @author apple
 *
 */
public class Account {

    private String id;        //账号
    private double balance;  //余额
    private String password;//密码
    
    /**
     * 构造器
     * @param id
     */
    public Account(String id,String password,double balance){
        this.id = id;
        this.password = password;
        this.balance = balance;
    }
    
    /**
     * 验证身份是否有效
     * @param password 密码
     * @return 密码匹配返回true,否则返回false
     */
    public boolean isValid(String password){
        return this.password.equals(password);
    }
    
    /**
     * 存钱
     * @param money 存款金额
     */
    public void deposit(double money){
        balance += money;
    }
    
    /**
     * 取钱
     * @param money 取款金额
     * @return 取款成功返回true,否则返回false
     */
    public boolean withdraw(double money){//取钱
        if (money <= balance) {
            balance -= money;
            return true;
        }
        return false;
    }
    
    /**
     * 获取账户ID
     * @return 账户ID
     */
    public String getId() {
        return id;
    }
    
    /**
     * 获取账户余额
     * @return 账户余额
     */
    public double getBalance() {
        return balance;
    }
    
    /**
     * 修改密码
     * @param password 密码
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    
}

在定义ATM类:

package org.mobiletrain;

/**
 * ATM机
 * @author apple
 *
 */
public class ATM {

    private Account currentAccount = null;
    //用一个数组模拟银行的数据库系统 预先保存若干个银行账户
    private Account[] accountsArray= {
            new Account("11223344","123123",1200),
            new Account("22334455","123456",3000),
            new Account("33445566","666666",5000)
    };
    
    /**
     * 读卡
     * @param account 银行账户
     * @return 读卡成功返回true,否则返回false
     */
    public boolean readCard(String id){
        for (Account account : accountsArray) {
            if (account.getId().equals(id)) {
                currentAccount = account;
                return true;
            }
        }
        return false;
    }
    
    /**
     * 验证身份
     * @param password 密码
     * @return 验证通过返回true,否则返回false
     */
    public boolean verify(String password){
        if (currentAccount != null) {
            return currentAccount.isValid(password);
        }
        return false;
    }
    
    /**
     * 查询余额
     * @return 当前余额
     */
    public double showBalance(){
        return currentAccount.getBalance();
    }
    
    /**
     * 存钱
     * @param money
     */
    public void deposit(int money){
        currentAccount.deposit(money);
    }
    
    /**
     * 取钱
     * @param money
     * @return 
     */
    public boolean withdraw(int money){
        return currentAccount.withdraw(money);
    }
    
    /**
     * 修改密码
     * @param newPassword 新密码
     */
    public void changePassword(String newPassword){
        currentAccount.setPassword(newPassword);
    }
    
    /**
     * 退卡
     */
    public void quitCard(){
        currentAccount = null;
    }
    
    /**
     * 转账
     * @param otherId 转入账号
     * @param money 转账金额
     * @return 转账成功返回true,否则返回false
     */
    public boolean transfer(String otherId,double money){
        for (Account account : accountsArray) {
            if (account.getId().equals(otherId)) {
                Account otherAccount = account;
                if (currentAccount.getBalance() >= money) {
                    currentAccount.withdraw(money);
                    otherAccount.deposit(money);
                    return true;
                }
                else {
                    return false;
                }
            }
        }
        return false;
    }
}

测试类:

package org.mobiletrain.test;

import java.util.Scanner;
import org.mobiletrain.ATM;

public class ATMtest {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);             
        ATM atm = new ATM();
        boolean isRunning = true;
        while(isRunning){
            System.out.println("欢迎使用XX银行ATM机");
            System.out.print("请插入银行卡:");
            String id = input.next();
            atm.readCard(id);
            if (atm.readCard(id)) {
                int counter = 0;
                boolean isCorrect = false;
                do{
                counter += 1;
                System.out.print("请输入密码:");
                String password = input.next();
                isCorrect = atm.verify(password);
                }while(counter < 3 && !isCorrect);
                if (isCorrect) {
                    boolean flag = true;
                    while(flag){
                        System.out.println("1.查询余额");
                        System.out.println("2.存款");
                        System.out.println("3.取款");
                        System.out.println("4.转账");
                        System.out.println("5.修改密码");
                        System.out.println("6.退卡");
                        int choice;
                        do {
                            System.out.print("请选择:");
                            choice = input.nextInt();
                        } while (choice < 1 || choice > 6);
                        switch (choice) {
                        case 1: 
                            System.out.println("账户余额:" + atm.showBalance());
                            break;
                        case 2:{//一堆花括号就构成了一个作用域(scope)
                                //在某个作用域中定义的变量只在该作用域中生效
                            System.out.print("请放入钞票:");
                            int money = input.nextInt();
                            atm.deposit(money);
                            System.out.println("存款成功!");
                            break;
                        }
                        case 3:{
                            System.out.print("请输入或选择取款金额:");
                            int money = input.nextInt();
                            if (atm.withdraw(money)) {
                                System.out.println("请取走你的钞票");
                            }
                            else {
                                System.out.println("余额不足!");
                            }
                            break;
                        }
                        case 4:{
                            System.out.print("请输入转入账号:");
                            String otherId = input.next();
                            System.out.print("请输入转账金额:");
                            double money = input.nextDouble();
                            if (atm.transfer(otherId, money)) {
                                System.out.println("转账成功");
                            }
                            else {
                                System.out.println("转账失败,请检查转入账号和账户余额是否正确!");
                            }
                            break;
                        }
                        case 5:
                            System.out.print("请输入新密码:");
                            String newPwd = input.next();
                            System.out.print("请再输入一次:");
                            String rePwd = input.next();
                            if (newPwd.equals(rePwd)) {
                                atm.changePassword(newPwd);
                                System.out.println("密码已修改");
                            }
                            else {
                                System.out.println("两次密码输入不一致.");
                            }
                            break;
                        case 6:
                            System.out.println("请取走你的卡片");
                            flag = false;
                            break;
                        }
                    }
                }
                else {
                    System.out.println("输入密码错误次数超过3次,你的卡被回收");
                }
            }
        }
        input.close();
    }
}
上一篇下一篇

猜你喜欢

热点阅读