8.10 方法重写/覆盖(override)
简单的说:方法覆盖(重写)就是子类有一个方法,和父类的某个方法的名称、返回类型、参数一样,那么我们就说子类的这个方法覆盖了父类的方法
8.10.1 快速入门、注意事项和使用细节
方法重写也叫方法覆盖,需要满足下面的条件 例如:如果父类是 public ,子类是 protected 就报错
父类代码:
package com.hspedu.override_;
public class Animal {
public void cry() {
System.out.println("动物叫唤..");
}
public Object m1() {
return null;
}
public String m2() {
return null;
}
public AAA m3() {
return null;
}
protected void eat() {
}
}
子类代码:
package com.hspedu.override_;
public class Dog extends Animal{
public void cry() {
System.out.println("小狗汪汪叫..");
}
public String m1() {
return null;
}
public void eat() { }
}
class AAA { }
class BBB extends AAA { }
主类代码:
package com.hspedu.override_;
public class Override01 {
public static void main(String[] args) {
Dog dog = new Dog();
dog.cry();
}
}
8.10.2 课堂练习
- 题1:请对方法的重写和重载做一个比较
- 题2
(1) 编写一个Person 类,包括属性/private(name、age),构造器、方法say(返回自我介绍的字符串)。 (2) 编写一个Student 类,继承Person 类,增加id、score 属性/private,以及构造器,定义say 方法(返回自我介绍的信息)。 (3) 在main 中,分别创建Person 和Student 对象,调用say 方法输出自我介绍。
Person父类代码:
package com.hspedu.override_;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String say() {
return "name=" + name + " age=" + age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student子类代码:
package com.hspedu.override_;
public class Student extends Person{
private int id;
private double score;
public Student(String name, int age, int id, double score) {
super(name, age);
this.id = id;
this.score = score;
}
public String say() {
return super.say() + " id=" + id + " score=" + score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
main代码:
package com.hspedu.override_;
public class OverrideExercise {
public static void main(String[] args) {
Person jack = new Person("jack", 10);
System.out.println(jack.say());
Student smith = new Student("smith", 20, 123456, 99.8);
System.out.println(smith.say());
}
}
8.11 面向对象编程-多态
8.11.1 先看一个问题
- 使用传统的方法来解决(private 属性)
- 传统的方法带来的问题是什么? 如何解决?
- 问题是: 代码的复用性不高,而且不利于代码维护
- 解决方案: 引出我们要讲解的多态
8.11.2 多[多种]态[状态]基本介绍
方法或对象具有多种形态。是面向对象的第三大特征,多态是建立在封装和继承基础之上的。
8.11.3 多态的具体体现
-
1、方法的多态PloyMethod.java 重写和重载就体现多态 [案例说明: ]
public class PloyMethod {
public static void main(String[] args) {
A a = new A();
System.out.println(a.sum(10, 20));
System.out.println(a.sum(10, 20, 30));
B b = new B();
a.say();
b.say();
}
}
class B {
public void say() {
System.out.println("B say() 方法被调用...");
}
}
class A extends B {
public int sum(int n1, int n2){
return n1 + n2;
}
public int sum(int n1, int n2, int n3){
return n1 + n2 + n3;
}
public void say() {
System.out.println("A say() 方法被调用...");
}
}
重要的几句话(记住): (1)一个对象的编译类型和运行类型可以不一致 (2)编译类型在定义对象时,就确定了,不能改变 (3)运行类型是可以变化的. (4)编译类型看定义时=号的左边,运行类型看=号的右边 代码解释:
public class zuoye {
public static void main(String[] args) {
Animal animal = new Dog();
animal.cry();
animal = new Cat();
animal.cry();
}
}
class Animal {
public void cry() {
System.out.println("Animal cry() 动物在叫....");
}
}
class Cat extends Animal {
public void cry() {
System.out.println("Cat cry() 小猫喵喵叫...");
}
}
class Dog extends Animal {
public void cry() {
System.out.println("Dog cry() 小狗汪汪叫...");
}
}
8.11.4 多态快速入门案例
使用多态的机制来解决主人喂食物的问题,走代码。Poly01.java
8.11.5 多态注意事项和细节讨论-向上转型 和 向下转向
com.hspedu.poly_.detail_ 包: PolyDetail.java
-
多态的前提是:两个对象(类)存在继承关系 -
多态的向上转型 1)本质:父类的引用指向了子类的对象 2)语法: 父类类型 引用名 = new 子类类型(); 3)特点:编译类型看左边,运行类型看右边。 可以调用父类中的所有成员(需遵守访问权限),不能调用子类中特有成员; 最终运行效果看子类(运行类型)的具体实现 ! 即调用方法时,按照从子类(运行类型)开始查找方法 ,然后调用,规则我前面我们讲的方法调用规则一致。 -
多态向下转型 语法:子类类型 引用名 = (子类类型) 父类引用; 2)只能强转父类的引用,不能强转父类的对象 3)要求父类的引用必须指向的是当前目标类型的对象 4)当向下转型后,可以调用子类类型中所有的成员
public class PolyDetail {
public static void main(String[] args) {
Animal animal = new Cat();
Object obj = new Cat();
animal.eat();
animal.run();
animal.show();
animal.sleep();
Cat cat = (Cat) animal;
cat.catchMouse();
System.out.println("ok~~");
}
}
class Animal {
String name = "动物";
int age = 10;
public void sleep(){
System.out.println("睡");
}
public void run(){
System.out.println("跑");
}
public void eat(){
System.out.println("吃");
}
public void show(){
System.out.println("hello,你好");
}
}
class Cat extends Animal {
public void eat(){
System.out.println("猫吃鱼");
}
public void catchMouse(){
System.out.println("猫抓老鼠");
}
}
class Dog extends Animal {
}
- 属性没有重写之说!属性的值看编译类型。PolyDetail02.java
public class PolyDetail02 {
public static void main(String[] args) {
Base base = new Sub();
System.out.println(base.count);
Sub sub = new Sub();
System.out.println(sub.count);
}
}
class Base {
int count = 10;
}
class Sub extends Base {
int count = 20;
}
- instanceof 比较操作符,用于判断对象的运行类型是否为XX 类型或XX 类型的子类型【举例说明】PolyDetail03.java
public class PolyDetail03 {
public static void main(String[] args) {
BB bb = new BB();
System.out.println(bb instanceof BB);
System.out.println(bb instanceof AA);
AA aa = new BB();
System.out.println(aa instanceof AA);
System.out.println(aa instanceof BB);
Object obj = new Object();
System.out.println(obj instanceof AA);
String str = "hello";
System.out.println(str instanceof Object);
}
}
class AA {}
class BB extends AA {}
8.11.6 课堂练习
- 1、请说出下面的每条语言,哪些是正确的,哪些是错误的,为什么?
- 2、
总结,访问属性看编译类型(左边),访问方法看运行类型(右边)
8.11.7 java的动态绑定机制(非常非常重要.)
Java 重要特性: 动态绑定机制
1.当调用对象方法的时候,该方法会和该对象的内存地址/运行类型绑定 2.当调用对象属性时,没有动态绑定机制,哪里声明,那里使用 代码:
public class DynamicBinding {
public static void main(String[] args) {
A a = new B();
System.out.println(a.sum());
System.out.println(a.sum1());
}
}
class A {
public int i = 10;
public int sum() {
return getI() + 10;
}
public int sum1() {
return i + 10;
}
public int getI() {
return i;
}
}
class B extends A {
public int i = 20;
public int getI() {
return i;
}
}
8.11.8 多态的应用
-
多态数组 com.hspedu.poly_.polyarr_ 包PloyArray.java 数组的定义类型为父类类型,里面保存的实际元素类型为子类类型 应用实例:现有一个继承结构如下:要求创建1 个Person 对象、2 个Student 对象和2 个Teacher 对象, 统一放在数组中,并调用每个对象say 方法. 应用实例升级:如何调用子类特有的方法,比如Teacher 有一个teach , Student 有一个study怎么调用?
主类代码:
package com.hspedu.poly_.polyarr_;
public class PloyArray {
public static void main(String[] args) {
Person[] persons = new Person[5];
persons[0] = new Person("jack", 20);
persons[1] = new Student("mary", 18, 100);
persons[2] = new Student("smith", 19, 30.1);
persons[3] = new Teacher("scott", 30, 20000);
persons[4] = new Teacher("king", 50, 25000);
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i].say());
if(persons[i] instanceof Student) {
Student student = (Student)persons[i];
student.study();
} else if(persons[i] instanceof Teacher) {
Teacher teacher = (Teacher)persons[i];
teacher.teach();
} else if(persons[i] instanceof Person){
} else {
System.out.println("你的类型有误, 请自己检查...");
}
}
}
}
Person父类代码:
package com.hspedu.poly_.polyarr_;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String say() {
return name + "\t" + age;
}
}
子类Student代码:
package com.hspedu.poly_.polyarr_;
public class Student extends Person {
private double score;
public Student(String name, int age, double score) {
super(name, age);
this.score = score;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String say() {
return "学生 " + super.say() + " score=" + score;
}
public void study() {
System.out.println("学生 " + getName() + " 正在学java...");
}
}
子类Teacher代码:
package com.hspedu.poly_.polyarr_;
public class Teacher extends Person {
private double salary;
public Teacher(String name, int age, double salary) {
super(name, age);
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String say() {
return "老师 " + super.say() + " salary=" + salary;
}
public void teach() {
System.out.println("老师 " + getName() + " 正在讲java课程...");
}
}
- 多态参数
主类代码:
package com.hspedu.poly_.polyparameter_;
public class PloyParameter {
public static void main(String[] args) {
Worker tom = new Worker("tom", 2500);
Manager milan = new Manager("milan", 5000, 200000);
PloyParameter ployParameter = new PloyParameter();
ployParameter.showEmpAnnual(tom);
ployParameter.showEmpAnnual(milan);
ployParameter.testWork(tom);
ployParameter.testWork(milan);
}
public void showEmpAnnual(Employee e) {
System.out.println(e.getAnnual());
}
public void testWork(Employee e) {
if(e instanceof Worker) {
((Worker) e).work();
} else if(e instanceof Manager) {
((Manager) e).manage();
} else {
System.out.println("不做处理...");
}
}
}
父类员工Employee代码:
package com.hspedu.poly_.polyparameter_;
public class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public double getAnnual() {
return 12 * salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
经理Manager代码:
package com.hspedu.poly_.polyparameter_;
public class Manager extends Employee{
private double bonus;
public Manager(String name, double salary, double bonus) {
super(name, salary);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public void manage() {
System.out.println("经理 " + getName() + " is managing");
}
@Override
public double getAnnual() {
return super.getAnnual() + bonus;
}
}
普通员工Worker代码:
package com.hspedu.poly_.polyparameter_;
public class Worker extends Employee {
public Worker(String name, double salary) {
super(name, salary);
}
public void work() {
System.out.println("普通员工 " + getName() + " is working");
}
@Override
public double getAnnual() {
return super.getAnnual();
}
}
8.12 Object 类详解
8.12.1 equals 方法
- 等号==和equals 的对比[面试题]
代码:
public class Equals01 {
public static void main(String[] args) {
A a = new A();
A b = a;
A c = b;
System.out.println(a == c);
System.out.println(b == c);
B bObj = a;
System.out.println(bObj == c);
int num1 = 10;
double num2 = 10.0;
System.out.println(num1 == num2);
"hello".equals("abc");
Integer integer1 = new Integer(1000);
Integer integer2 = new Integer(1000);
System.out.println(integer1 == integer2);
System.out.println(integer1.equals(integer2));
String str1 = new String("hspedu");
String str2 = new String("hspedu");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
}
}
class B {}
class A extends B {}
8.12.2 如何重写equals 方法
应用实例: 判断两个Person 对象的内容是否相等,如果两个Person 对象的各个属性值都一样,则返回true,反之false。
package com.hspedu.object_;
public class EqualsExercise01 {
public static void main(String[] args) {
Person person1 = new Person("jack", 10, '男');
Person person2 = new Person("jack", 10, '男');
System.out.println(person1.equals(person2));
}
}
class Person{
private String name;
private int age;
private char gender;
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(obj instanceof Person) {
Person p = (Person)obj;
return this.name.equals(p.name) && this.age == p.age && this.gender == p.gender;
}
return false;
}
public Person(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
}
8.12.3 课堂练习题
- 练习1
注意: System.out.println(p1.name .equals( p2.name));//T p1.name是字符串,已经重写了 System.out.println(p1.equals(p2));//False 自己定义的Person没有重写
public class EqualsExercise02 {
public static void main(String[] args) {
Person_ p1 = new Person_();
p1.name = "hspedu";
Person_ p2 = new Person_();
p2.name = "hspedu";
System.out.println(p1==p2);
System.out.println(p1.name .equals( p2.name));
System.out.println(p1.equals(p2));
String s1 = new String("asdf");
String s2 = new String("asdf");
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
}
}
class Person_{
public String name;
}
- 2、练习2
8.12.4 hashCode 方法
-
提高具有哈希结构的容器的效率! -
两个引用,如果指向的是同一个对象,则哈希值肯定是一样的! -
两个引用,如果指向的是不同对象,则哈希值是不一样的 -
哈希值主要根据地址号来的!, 不能完全将哈希值等价于地址。 -
案例演示[HashCode_.java]: obj.hashCode() [测试:A obj1 = new A(); A obj2 = new A(); A obj3 = obj1] -
后面在集合,中hashCode 如果需要的话,也会重写, 在讲解集合时,再说如何重写hashCode()
public class HashCode_ {
public static void main(String[] args) {
AA aa = new AA();
AA aa2 = new AA();
AA aa3 = aa;
System.out.println("aa.hashCode()=" + aa.hashCode());
System.out.println("aa2.hashCode()=" + aa2.hashCode());
System.out.println("aa3.hashCode()=" + aa3.hashCode());
}
}
class AA {}
8.12.5 toString 方法
-
基本介绍 默认返回:全类名+@+哈希值的十六进制,【查看Object 的toString 方法】 子类往往重写toString 方法,用于返回对象的属性信息 -
重写toString 方法,打印对象或拼接对象时,都会自动调用该对象的toString 形式. 案例演示:Monster [name, job, sal] 案例: ToString_.java 重写toString方法, 输出对象的属性 使用快捷键即可 alt+insert -> toString 重写后,一般是把对象的属性值输出,当然程序员也可以自己定制 -
当直接输出一个对象时, toString 方法会被默认的调用, 比如System.out.println(monster) ; 就会默认调用monster.toString()
public class ToString_ {
public static void main(String[] args) {
Monster monster = new Monster("小妖怪", "巡山的", 1000);
System.out.println(monster.toString() + " hashcode=" + monster.hashCode());
System.out.println("==当直接输出一个对象时,toString 方法会被默认的调用==");
System.out.println(monster);
}
}
class Monster {
private String name;
private String job;
private double sal;
public Monster(String name, String job, double sal) {
this.name = name;
this.job = job;
this.sal = sal;
}
@Override
public String toString() {
return "Monster{" +
"name='" + name + '\'' +
", job='" + job + '\'' +
", sal=" + sal +
'}';
}
@Override
protected void finalize() throws Throwable {
System.out.println("fin..");
}
}
8.12.6 finalize 方法
- 当对象被回收时,系统自动调用该对象的finalize 方法。子类可以重写该方法,做一些释放资源的操作【演示】
- 什么时候被回收:当某个对象没有任何引用时,则 jvm 就认为这个对象是一个垃圾对象,就会使用垃圾回收机制来销毁该对象,在销毁该对象前,会先调用finalize 方法。
- 垃圾回收机制的调用,是由系统来决定(即有自己的GC 算法), 也可以通过System.gc() 主动触发垃圾回收机制,测试:Car [name]
提示: 我们在实际开发中,几乎不会运用finalize , 所以更多就是为了应付面试.
public class Finalize_ {
public static void main(String[] args) {
Car bmw = new Car("宝马");
bmw = null;
System.gc();
System.out.println("程序退出了....");
}
}
class Car {
private String name;
public Car(String name) {
this.name = name;
}
@Override
protected void finalize() throws Throwable {
System.out.println("我们销毁 汽车" + name );
System.out.println("释放了某些资源...");
}
}
8.13 断点调试(debug)
8.13.1 一个实际需求
1.在开发中,新手程序员在查找错误时,这时老程序员就会温馨提示,可以用断点调试,一步一步的看源码执行的过程,从而发现错误所在。 2.重要提示:在断点调试过程中,是运行状态,是以对象的运行类型来执行的.A extends B; Bb = new A0); b.xx0;
8.13.2 断点调试介绍
- 断点调试是指在程序的某一行设置一个断点,调试时,程序运行到这行就会停住,然后你可以一步一步往下调试,调试过程中可以看各个变量当前的值,出错的话,调试到出错的代码行即显示错误,停下。进行分析从而找到这个Bug。
- 断点调试是程序员必须掌握的技能。
- 断点调试也能帮助我们查看java底层源代码的执行过程,提高程序员的Java水平.
8.13.3 断点调试的快捷键
F7(跳入) F8(跳过) shift+F8(跳出) F9(resume,执行到下一个断点) F7:跳入方法内 F8: 逐行执行代码. shift+F8: 跳出方法
工作区域图:
8.13.4 断点调试应用案例
- 案例1 com.hspedu.debug_ 包Debug01.java
看一下变量的变化情况等,右键Debug或者直接点F8
public class Debug01 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
System.out.println("i=" + i);
System.out.println("sum=" + i);
}
System.out.println("退出for....");
}
}
- 案例2
看一下数组越界的异常Debug02.java arr.length故意写错
package com.hspedu.debug_;
public class Debug02 {
public static void main(String[] args) {
int[] arr = {1, 10, -1};
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("退出for");
}
}
- 案例3
演示如何追源码,看看java 设计者是怎么实现的。(提高编程思想)。 小技巧:将光标放在某个变量上,可以看到最新的数据。Debug03.java
import java.util.Arrays;
public class Debug03 {
public static void main(String[] args) {
int[] arr = {1, -1, 10, -20 , 100};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
F7跳入,Shift+F8一层一层的跳回
- 案例4
演示如何直接执行到下一个断点 F9 resume。 小技巧: 断点可以在debug 过程中,动态的下断点
import java.util.Arrays;
public class Debug04 {
public static void main(String[] args) {
int[] arr = {1, -1, 10, -20 , 100};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println("hello100");
System.out.println("hello200");
System.out.println("hello300");
System.out.println("hello400");
System.out.println("hello500");
System.out.println("hello600");
System.out.println("hello700");
}
}
8.13.5 断点调试课后练习
DebugExercise.java 1.使用断点调试的方法,追踪一个对象创建的过程。Person [name ,age,构造器…] 2.我们使用断点调试,查看动态绑定机制的如何工作
public class DebugExercise {
public static void main(String[] args) {
Person jack = new Person("jack", 20);
System.out.println(jack);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
8.14 项目-零钱通
8.14.1项目需求说明
使用Java 开发零钱通项目, 可以完成收益入账,消费,查看明细,退出系统等功能.
编写文件SmallChangeSys.java 完成基本功能(过程编程) 老师提示:先使用过程编程,后面改成OOP 版本,请小伙伴体会OOP 编程带来的好处
8.14.2 项目的界面
化繁为简.
- 先完成显示菜单,并可以选择
- 完成零钱通明细.
- 完成收益入账
- 消费
- 退出
8.14.3 项目代码实现与改进(OOP)
实现代码:
- 先完成显示菜单,并可以选择
package com.cmLianXi.SmallChangeSys;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
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 (key) {
case "1":
System.out.println("1 零钱通明细");
break;
case "2":
System.out.println("2 收益入账");
break;
case "3":
System.out.println("3 消费");
break;
case "4":
System.out.println("4 退出系统");
loop = false;
break;
default:
System.out.println("输入有误,请重新输入");
}
} while (loop);
System.out.println("===== 退出系统======");
}
}
- 完成零钱通明细.
- 完成收益入账
package com.cmLianXi.SmallChangeSys;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
String details = "---------------零钱通明细------------------";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
do {
System.out.println("\n=============零钱通=============");
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 (key) {
case "1":
System.out.println(details);
break;
case "2":
System.out.println("收益入账金额");
money = scanner.nextDouble();
balance += money;
date = new Date();
details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
case "3":
System.out.println("3 消费");
break;
case "4":
System.out.println("4 退出系统");
loop = false;
break;
default:
System.out.println("输入有误,请重新输入");
}
} while (loop);
System.out.println("===== 退出系统======");
}
}
- 消费
package com.cmLianXi.SmallChangeSys;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
String details = "---------------零钱通明细------------------";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String note = "";
do {
System.out.println("\n=============零钱通=============");
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 (key) {
case "1":
System.out.println(details);
break;
case "2":
System.out.println("收益入账金额");
money = scanner.nextDouble();
balance += money;
date = new Date();
details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
case "3":
System.out.println("消费金额");
money = scanner.nextDouble();
System.out.print("消费说明:");
note = scanner.next();
balance -= money;
date = new Date();
details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
case "4":
System.out.println("4 退出系统");
loop = false;
break;
default:
System.out.println("输入有误,请重新输入");
}
} while (loop);
System.out.println("===== 退出系统======");
}
}
- 改进:用户输入4退出时,给出提示"你确定要退出吗? y/n",必须输入正确的y/n ,否则循环输入指令,直到输入y 或者 n
第一种方案,建议使用。(建议一段代码,完成一个小功能,尽量不要混在一起)
package com.cmLianXi.SmallChangeSys;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
String details = "---------------零钱通明细------------------";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String note = "";
do {
System.out.println("\n=============零钱通=============");
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 (key) {
case "1":
System.out.println(details);
break;
case "2":
System.out.println("收益入账金额");
money = scanner.nextDouble();
balance += money;
date = new Date();
details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
case "3":
System.out.println("消费金额");
money = scanner.nextDouble();
System.out.print("消费说明:");
note = scanner.next();
balance -= money;
date = new Date();
details += "\n" + note + "\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("===== 退出系统======");
}
}
第二种方案,就写一个while:
package com.cmLianXi.SmallChangeSys;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
String details = "---------------零钱通明细------------------";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String note = "";
do {
System.out.println("\n=============零钱通=============");
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 (key) {
case "1":
System.out.println(details);
break;
case "2":
System.out.println("收益入账金额");
money = scanner.nextDouble();
balance += money;
date = new Date();
details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
case "3":
System.out.println("消费金额");
money = scanner.nextDouble();
System.out.print("消费说明:");
note = scanner.next();
balance -= money;
date = new Date();
details += "\n" + note + "\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)) {
loop = false;
break;
} else if ("n".equals(choice)) {
break;
}
}
default:
System.out.println("输入有误,请重新输入");
}
} while (loop);
System.out.println("===== 退出系统======");
}
}
- 在收益入账和消费时,判断金额是否合理,并给出相应的提示(完成所有功能)。
package com.hspedu.smallchange;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
String details = "-----------------零钱通明细------------------";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String note = "";
do {
System.out.println("\n================零钱通菜单===============");
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.print("请选择(1-4): ");
key = scanner.next();
switch (key) {
case "1":
System.out.println(details);
break;
case "2":
System.out.print("收益入账金额:");
money = scanner.nextDouble();
if(money <= 0) {
System.out.println("收益入账金额 需要 大于 0");
break;
}
balance += money;
date = new Date();
details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
case "3":
System.out.print("消费金额:");
money = scanner.nextDouble();
if(money <= 0 || money > balance) {
System.out.println("你的消费金额 应该在 0-" + balance);
break;
}
System.out.print("消费说明:");
note = scanner.next();
balance -= money;
date = new Date();
details += "\n" + note + "\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("-----退出了零钱通项目-----");
}
}
使用OOP面向对象编程:
package com.hspedu.smallchange.oop;
public class SmallChangeSysApp {
public static void main(String[] args) {
System.out.println("====hello公司====");
new SmallChangeSysOOP().mainMenu();
}
}
以下代码是完成零钱通的各个功能的类:
package com.hspedu.smallchange.oop;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSysOOP {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
String details = "-----------------零钱通明细------------------";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String note = "";
public void mainMenu() {
do {
System.out.println("\n================零钱通菜单(OOP)===============");
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.print("请选择(1-4): ");
key = scanner.next();
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.print("收益入账金额:");
money = scanner.nextDouble();
if(money <= 0) {
System.out.println("收益入账金额 需要 大于 0");
return;
}
balance += money;
date = new Date();
details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
}
public void pay() {
System.out.print("消费金额:");
money = scanner.nextDouble();
if(money <= 0 || money > balance) {
System.out.println("你的消费金额 应该在 0-" + balance);
return;
}
System.out.print("消费说明:");
note = scanner.next();
balance -= money;
date = new Date();
details += "\n" + note + "\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;
}
}
}
|