Java 自学
2021/9/26 14:40:40
本文主要是介绍Java 自学,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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(); } }
这篇关于Java 自学的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-28知识管理革命:文档软件的新玩法了解一下!
- 2024-11-28低代码应用课程:新手入门全攻略
- 2024-11-28哪些办公软件适合团队协作,且能够清晰记录每个阶段的工作进展?
- 2024-11-28全栈低代码开发课程:零基础入门到初级实战
- 2024-11-28拖动排序课程:轻松掌握课程拖动排序功能
- 2024-11-28如何高效管理数字化转型项目
- 2024-11-28SMART法则好用吗?有哪些项目管理工具辅助实现?
- 2024-11-28深度剖析:6 款办公软件如何构建设计团队项目可视化管理新生态?
- 2024-11-28HTTP缓存课程:新手入门指南
- 2024-11-28实战丨证券 HTAP 混合业务场景的难点问题应对