2021/9/25
韩顺平 零基础30天学会Java
p307-p343
对象的多态:对象的编译类型和运算类型不一致
多态的向上转型:父类的引用指向子类的对象
向下转型:子类类型 引用名=(子类类型)父类引用
instanceof:对象的运行类型是否为xx类型或其子类型
java的动态绑定机制
多态数组
多态参数
equals方法
hashcode方法
toString方法
finalize方法
断点调试:运行类型
零钱通项目:使用java开发零钱通项目,可以完成收益入账、消费、查看明细、退出系统等功能
化繁为简
1.完成显示菜单
2.完成零钱通明细
3.完成收益入账
4.消费
5.退出
6.代码改进
7.改进(oop)面向对象
面向过程
public class SmallChange { public static void main(String[] args) { //1.显示菜单 boolean loop = true; Scanner scanner = new Scanner(System.in); String key = ""; //2.零钱通明细 String details = "------------零钱通明细------------"; //3.收益入账 double money = 0; double balance = 0; Date date = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //4.消费 String note = ""; //5.退出 do { System.out.println("============零钱通菜单============"); System.out.println("\t\t\t1.零钱通明细"); System.out.println("\t\t\t2.收益入账"); System.out.println("\t\t\t3.消费"); System.out.println("\t\t\t4.退 出"); System.out.println("请输入(1-4):"); key = scanner.next(); //switch语句 switch (key) { case "1": System.out.println(details); break; case "2": System.out.println("收益入账金额:"); money = scanner.nextDouble(); if (money <= 0) { System.out.println("收益入账金额 需要 大于 0"); break; } balance += money; date = new Date(); details += "\n收益入账\t\t+" + money + "\t" + sdf.format(date) + "\t" + balance; break; case "3": System.out.println("消费金额:"); money = scanner.nextDouble(); if (money <= 0 || money > balance) { System.out.println("你的消费金额 应该在 0-" + balance); break; } System.out.println("消费说明:"); note = scanner.next(); balance -= money; date = new Date(); details += "\n" + note + "\t\t-" + money + "\t" + sdf.format(date) + "\t" + balance; break; case "4": String choice = ""; while (true) { System.out.println("你确定要输出吗?y/n"); choice = scanner.next(); if ("y".equals(choice) || "n".equals(choice)) { break; } } if (choice.equals("y")) { loop = false; } break; default: System.out.println("选择有误,请重新选择"); } } while (loop); System.out.println("-----退出了零钱通系统----"); } }
面向对象
public class SmallChangeOop { //1.显示菜单 boolean loop = true; Scanner scanner = new Scanner(System.in); String key = ""; //2.零钱通明细 String details = "------------零钱通明细------------"; //3.收益入账 double money = 0; double balance = 0; Date date = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //4.消费 String note = ""; //5.退出 public void mainMenu() { System.out.println("显示零钱通菜单--"); do { System.out.println("============零钱通菜单============"); System.out.println("\t\t\t1.零钱通明细"); System.out.println("\t\t\t2.收益入账"); System.out.println("\t\t\t3.消费"); System.out.println("\t\t\t4.退 出"); System.out.println("请输入(1-4):"); key = scanner.next(); //switch语句 switch (key) { case "1": this.detail(); break; case "2": this.income(); break; case "3": this.pay(); break; case "4": this.exit(); break; default: System.out.println("选择有误,请重新选择"); } } while (loop); } public void detail() { System.out.println(details); } public void income() { System.out.println("收益入账金额:"); money = scanner.nextDouble(); if (money <= 0) { System.out.println("收益入账金额 需要 大于 0"); return; } balance += money; date = new Date(); details += "\n收益入账\t\t+" + money + "\t" + sdf.format(date) + "\t" + balance; } public void pay() { System.out.println("消费金额:"); money = scanner.nextDouble(); if (money <= 0 || money > balance) { System.out.println("你的消费金额 应该在 0-" + balance); return; } System.out.println("消费说明:"); note = scanner.next(); balance -= money; date = new Date(); details += "\n" + note + "\t\t-" + money + "\t" + sdf.format(date) + "\t" + balance; } public void exit() { String choice = ""; while (true) { System.out.println("你确定要输出吗?y/n"); choice = scanner.next(); if ("y".equals(choice) || "n".equals(choice)) { break; } } if (choice.equals("y")) { loop = false; } }
test测试
public class Test { public static void main(String[] args){ new SmallChangeOop().mainMenu(); } }