• 41-面向对象编程(中级部分)-2


    方法重写/覆盖

    基本介绍

    简单的说:方法覆盖(重写)就是子类有一个方法,和父类的某个方法的名称,返回类型,参数一样,那么我们就说这个方法覆盖了父类的方法
    请添加图片描述

    package com.hspedu.override_;
    
    public class Override01 {
        public static void main(String[] args) {
            //演示方法重写的情况
            Dog dog = new Dog();
            dog.cry();//ctrl+b
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    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() {
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    package com.hspedu.override_;
    
    public class Dog extends Animal{
        //老韩解读
        //1. 因为Dog 是 Animal子类
        //2. Dog的 cry方法和 Animal的 cry定义形式一样(名称、返回类型、参数)
        //3. 这时我们就说 Dog的cry方法,重写了Animal的cry方法
        public void cry() {
            System.out.println("小狗汪汪叫..");
        }
    
        //细节: 子类方法的返回类型和父类方法返回类型一样,
        //      或者是父类返回类型的子类比如 父类 返回类型是 Object ,
        //      子类方法返回类型是String
        public String m1() {
            return null;
        }
        //这里Object 不是 String的子类,因此编译错误
    //    public Object m2() {
    //        return null;
    //    }
    
    //    public BBB m3() {
    //        return null;
    //    }
        //细节: 子类方法不能缩小父类方法的访问权限 【演示】
        //public > protected > 默认>private
        public void eat() {
    
        }
    }
    
    class AAA {
    
    }
    
    class BBB extends AAA  {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    重载与重写的区别

    请添加图片描述

    练习

    请添加图片描述

    package com.hspedu.override_;
    
    public class OverrideExercise {
        public static void main(String[] args) {
            //在main中,分别创建Person和Student对象,调用say方法输出自我介绍
            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());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    package com.hspedu.override_;
    
    //编写一个Person类,包括属性/private(name、age),构造器、方法say(返回自我介绍的字符串)
    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    package com.hspedu.override_;
    
    
    //编写一个Student类,继承Person类,增加id、score属性/private,以及构造器,定义say方法(返回自我介绍的信息)。
    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;
        }
        //say
        public String say() { //这里体现super的一个好处,代码复用.
            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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    多态

    基本介绍

    方法或对象具有多种形态。是卖你想对象的第三大特征,多态是建立在封装和继承基础之上的

    多态的具体体现

    1. 方法的多态
      重写和重载体现多态
    package com.hspedu.poly_;
    
    public class PloyMethod {
        public static void main(String[] args) {
            //方法重载体现多态
            A a = new A();
            //这里我们传入不同的参数,就会调用不同sum方法,就体现多态
            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){//和下面sum 构成重载
            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
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    1. 对象的多态
    • 一个对象的编译类型和运行类型可以不一致
    • 编译类型在定义对象时,就确定了,不能改变
    • 运行类型时可以变化的
    • 编译类型看定义时 = 号的左边,运行类型看 = 号的右边
      eg:
      请添加图片描述
    package com.hspedu.poly_.objectpoly_;
    
    public class PolyObject {
        public static void main(String[] args) {
            //体验对象多态特点
            //animal 编译类型就是 Animal , 运行类型 Dog
            Animal animal = new Dog();
            //因为运行时 , 执行到改行时,animal运行类型是Dog,所以cry就是Dog的cry
            animal.cry(); //小狗汪汪叫
    
            //animal 编译类型 Animal,运行类型就是 Cat
            animal = new Cat();
            animal.cry(); //小猫喵喵叫
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    package com.hspedu.poly_.objectpoly_;
    
    public class Animal {
        public void  cry() {
            System.out.println("Animal cry() 动物在叫....");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    package com.hspedu.poly_.objectpoly_;
    
    public class Cat extends Animal {
    
        public void cry() {
            System.out.println("Cat cry() 小猫喵喵叫...");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    package com.hspedu.poly_.objectpoly_;
    
    public class Dog extends Animal {
    
        public void cry() {
            System.out.println("Dog cry() 小狗汪汪叫...");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    快速入门

    使用多态的机制来解决主人喂食物的问题

    package com.hspedu.poly_;
    
    public class Poly01 {
        public static void main(String[] args) {
    
            Master tom = new Master("汤姆");
            Dog dog = new Dog("大黄~");
            Bone bone = new Bone("大棒骨~");
            tom.feed(dog, bone);
    
            Cat cat = new Cat("小花猫~");
            Fish fish = new Fish("黄花鱼~");
            System.out.println("===========-------");
            tom.feed(cat, fish);
    
            //添加 给小猪为米饭
            Pig pig = new Pig("小花猪");
            Rice rice = new Rice("米饭");
            System.out.println("===================");
            tom.feed(pig, rice); //注意这里传入的参数
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    package com.hspedu.poly_;
    
    public class Master {
        private String name;
    
        public Master(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        //使用多态机制,可以统一的管理主人喂食的问题
        //animal 编译类型是Animal,可以指向(接收) Animal子类的对象
        //food 编译类型是Food ,可以指向(接收) Food子类的对象
        public void feed(Animal animal, Food food) {   //这里属于向上转型
            System.out.println("主人 " + name + " 给 " + animal.getName() + " 吃 " + food.getName());
        }
    
        //主人给小狗 喂食 骨头
    //    public void feed(Dog dog, Bone bone) {
    //        System.out.println("主人 " + name + " 给 " + dog.getName() + " 吃 " + bone.getName());
    //    }
    //    //主人给 小猫喂 黄花鱼
    //    public void feed(Cat cat, Fish fish) {
    //        System.out.println("主人 " + name + " 给 " + cat.getName() + " 吃 " + fish.getName());
    //    }
    
        //如果动物很多,食物很多
        //===> feed 方法很多,不利于管理和维护
        //Pig --> Rice
        //Tiger ---> meat ...
        //...
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    package com.hspedu.poly_;
    
    public class Animal {
        private String name;
    
        public Animal(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    package com.hspedu.poly_;
    
    public class Cat extends Animal {
        public Cat(String name) {
            super(name);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    package com.hspedu.poly_;
    
    public class Dog extends Animal {
        public Dog(String name) {
            super(name);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    package com.hspedu.poly_;
    
    public class Food {
        private String name;
    
        public Food(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    package com.hspedu.poly_;
    
    public class Fish extends Food {
        public Fish(String name) {
            super(name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    package com.hspedu.poly_;
    
    public class Rice extends Food {
        public Rice(String name) {
            super(name);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    多态注意事项和细节讨论

    • 多态的前提是:两个对象(类)存在继承关系
    • 多态的向上转型
    1. 本质:父类的引用指向了子类的对象
    2. 语法:父类类型 引用名 = new 子类类型();
    3. 特点:编译类型看左边,运行类型看右边。可以调用父类中的所有成员(须遵守访问权限);
      不能调用子类中的特有成员
      最终运行效果看子类的具体实现
    • 多态向下转型:
    1. 语法:子类类型 引用名 = (子类类型)父类引用;
    2. 只能强转父类的引用,不能强转父类的对象
    3. 要求父类的引用必须指向的是当前目标类型的对象
    4. 可以调用子类类型中所有的成员
    package com.hspedu.poly_.detail_;
    
    public class PolyDetail {
        public static void main(String[] args) {
    
            //向上转型: 父类的引用指向了子类的对象
            //语法:父类类型引用名 = new 子类类型();
            Animal animal = new Cat();
            Object obj = new Cat();//可以吗? 可以 Object 也是 Cat的父类
    
            //向上转型调用方法的规则如下:
            //(1)可以调用父类中的所有成员(需遵守访问权限)
            //(2)但是不能调用子类的特有的成员
            //(#)因为在编译阶段,能调用哪些成员,是由编译类型来决定的
            //animal.catchMouse();错误
            //(4)最终运行效果看子类(运行类型)的具体实现, 即调用方法时,按照从子类(运行类型)开始查找方法
            //,然后调用,规则我前面我们讲的方法调用规则一致。
            animal.eat();//猫吃鱼..
            animal.run();//跑
            animal.show();//hello,你好
            animal.sleep();//睡
    
            //老师希望,可以调用Cat的 catchMouse方法
            //多态的向下转型
            //(1)语法:子类类型 引用名 =(子类类型)父类引用;
            //问一个问题? cat 的编译类型 Cat,运行类型是 Cat
            Cat cat = (Cat) animal;
            cat.catchMouse();//猫抓老鼠
            //(2)要求父类的引用必须指向的是当前目标类型的对象
            Dog dog = (Dog) animal; //可以吗?
    
            System.out.println("ok~~");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    package com.hspedu.poly_.detail_;
    
    public 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,你好");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    package com.hspedu.poly_.detail_;
    
    public class Cat extends Animal {
        public void eat(){//方法重写
            System.out.println("猫吃鱼");
        }
        public void catchMouse(){//Cat特有方法
            System.out.println("猫抓老鼠");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    package com.hspedu.poly_.detail_;
    
    public class Dog extends Animal {//Dog是Animal的子类
    }
    
    • 1
    • 2
    • 3
    • 4
    • 属性没有重写之说!属性的值看编译类型
    package com.hspedu.poly_.detail_;
    
    public class PolyDetail02 {
        public static void main(String[] args) {
            //属性没有重写之说!属性的值看编译类型
            Base base = new Sub();//向上转型
            System.out.println(base.count);// ? 看编译类型 10
            Sub sub = new Sub();
            System.out.println(sub.count);//?  20
        }
    }
    
    class Base { //父类
        int count = 10;//属性
    }
    class Sub extends Base {//子类
        int count = 20;//属性
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • instanceof 比较操作符,用于判断对象的运行类型是否为XX类型或XX类型的子类类型
    package com.hspedu.poly_.detail_;
    
    public class PolyDetail03 {
        public static void main(String[] args) {
            BB bb = new BB();
            System.out.println(bb instanceof  BB);// true
            System.out.println(bb instanceof  AA);// true
    
            //aa 编译类型 AA, 运行类型是BB
            //BB是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);//false
            String str = "hello";
            //System.out.println(str instanceof AA);
            System.out.println(str instanceof Object);//true
        }
    }
    
    class AA {} //父类
    class BB extends AA {}//子类
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    练习

    请添加图片描述
    请添加图片描述

    java的动态绑定机制

    1. 当调用对象方法的时候,该方法会和对象的内存地址/运行类型绑定
    2. 当调用对象属性时,没有动态绑定机制,哪里声明,那里使用
    package com.hspedu.poly_.dynamic_;
    
    public class DynamicBinding {
        public static void main(String[] args) {
            //a 的编译类型 A, 运行类型 B
            A a = new B();//向上转型
            System.out.println(a.sum());//?40 -> 30
            System.out.println(a.sum1());//?30-> 20
        }
    }
    
    class A {//父类
        public int i = 10;
        //动态绑定机制:
    
        public int sum() {//父类sum()
            return getI() + 10;//20 + 10
        }
    
        public int sum1() {//父类sum1()
            return i + 10;//10 + 10
        }
    
        public int getI() {//父类getI
            return i;
        }
    }
    
    class B extends A {//子类
        public int i = 20;
    
    //    public int sum() {
    //        return i + 20;
    //    }
    
        public int getI() {//子类getI()
            return i;
        }
    
    //    public int sum1() {
    //        return i + 10;
    //    }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    多态的应用

    多态数组

    数组的定义类型为父类类型,里面保存的实际元素为子类类型
    案例:
    请添加图片描述

    package com.hspedu.poly_.polyarr_;
    
    public class PloyArray {
        public static void main(String[] args) {
            //应用实例:现有一个继承结构如下:要求创建1个Person对象、
            // 2个Student 对象和2个Teacher对象, 统一放在数组中,并调用每个对象say方法
    
            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);
    
            //循环遍历多态数组,调用say
            for (int i = 0; i < persons.length; i++) {
                //老师提示: person[i] 编译类型是 Person ,运行类型是是根据实际情况有JVM来判断
                System.out.println(persons[i].say());//动态绑定机制
                //这里大家聪明. 使用 类型判断 + 向下转型.
                if(persons[i]  instanceof  Student) {//判断person[i] 的运行类型是不是Student
                    Student student = (Student)persons[i];//向下转型
                    student.study();
                    //小伙伴也可以使用一条语句 ((Student)persons[i]).study();
                } else if(persons[i] instanceof  Teacher) {
                    Teacher teacher = (Teacher)persons[i];
                    teacher.teach();
                } else if(persons[i] instanceof  Person){
                    //System.out.println("你的类型有误, 请自己检查...");
                } else {
                    System.out.println("你的类型有误, 请自己检查...");
                }
    
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    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;
        }
        //重写父类say
    
        @Override
        public String say() {
            return "学生 " + super.say() + " score=" + score;
        }
        //特有的方法
        public void study() {
            System.out.println("学生 " + getName() + " 正在学java...");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    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;
        }
        //写重写父类的say方法
    
        @Override
        public String say() {
            return "老师 " + super.say() + " salary=" + salary;
        }
        //特有方法
        public void teach() {
            System.out.println("老师 " + getName() + " 正在讲java课程...");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    多态参数

    请添加图片描述

    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);
    
        }
    
        //showEmpAnnual(Employee e)
        //实现获取任何员工对象的年工资,并在main方法中调用该方法 [e.getAnnual()]
        public void showEmpAnnual(Employee e) {
            System.out.println(e.getAnnual());//动态绑定机制.
        }
        //添加一个方法,testWork,如果是普通员工,则调用work方法,如果是经理,则调用manage方法
        public void testWork(Employee e) {
            if(e instanceof  Worker) {
                ((Worker) e).work();//有向下转型操作
            } else if(e instanceof Manager) {
                ((Manager) e).manage();//有向下转型操作
            } else {
                System.out.println("不做处理...");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    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();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    Objiect 类详解

    equals

    • == 和 equals的对比
      请添加图片描述请添加图片描述
    package com.hspedu.object_;
    
    public class Equals01 {
    
        public static void main(String[] args) {
            A a = new A();
            A b = a;
            A c = b;
            System.out.println(a == c);//true
            System.out.println(b == c);//true
            B bObj = a;
            System.out.println(bObj == c);//true
            int num1 = 10;
            double num2 = 10.0;
            System.out.println(num1 == num2);//基本数据类型,判断值是否相等
    
            //equals 方法,源码怎么查看.
            //把光标放在equals方法,直接输入ctrl+b
            //如果你使用不了. 自己配置. 即可使用.
    
            /*
            //带大家看看Jdk的源码 String类的 equals方法
            //把Object的equals方法重写了,变成了比较两个字符串值是否相同
            public boolean equals(Object anObject) {
            if (this == anObject) {//如果是同一个对象
                return true;//返回true
            }
            if (anObject instanceof String) {//判断类型
                String anotherString = (String)anObject;//向下转型
                int n = value.length;
                if (n == anotherString.value.length) {//如果长度相同
                    char v1[] = value;
                    char v2[] = anotherString.value;
                    int i = 0;
                    while (n-- != 0) {//然后一个一个的比较字符
                        if (v1[i] != v2[i])
                            return false;
                        i++;
                    }
                    return true;//如果两个字符串的所有字符都相等,则返回true
                }
            }
            return false;//如果比较的不是字符串,则直接返回false
        }
             */
    
    
            "hello".equals("abc");
    
            //看看Object类的 equals 是
            /*
            //即Object 的equals 方法默认就是比较对象地址是否相同
            //也就是判断两个对象是不是同一个对象.
             public boolean equals(Object obj) {
                return (this == obj);
            }
             */
    
    
            /*
            //从源码可以看到 Integer 也重写了Object的equals方法,
            //变成了判断两个值是否相同
            public boolean equals(Object obj) {
                if (obj instanceof Integer) {
                    return value == ((Integer)obj).intValue();
                }
                return false;
            }
             */
            Integer integer1 = new Integer(1000);
            Integer integer2 = new Integer(1000);
            System.out.println(integer1 == integer2);//false
            System.out.println(integer1.equals(integer2));//true
    
            String str1 = new String("hspedu");
            String str2 = new String("hspedu");
            System.out.println(str1 == str2);//false
            System.out.println(str1.equals(str2));//true
    
    
    
    
        }
    }
    
    class B {}
    class A extends B {}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 练习:
      请添加图片描述
    package com.hspedu.object_;
    
    public class EqualsExercise01 {
        public static void main(String[] args) {
            Person person1 = new Person("jack", 10, '男');
            Person person2 = new Person("jack", 20, '男');
    
            System.out.println(person1.equals(person2));//假
        }
    }
    //判断两个Person对象的内容是否相等,
    //如果两个Person对象的各个属性值都一样,则返回true,反之false
    class Person{ //extends Object
        private String name;
        private int age;
        private char gender;
    
        //重写Object 的 equals方法
        public boolean equals(Object obj) {
            //判断如果比较的两个对象是同一个对象,则直接返回true
            if(this == obj) {
                return true;
            }
            //类型判断
            if(obj instanceof  Person) {//是Person,我们才比较
    
                //进行 向下转型, 因为我需要得到obj的 各个属性
                Person p = (Person)obj;
                return this.name.equals(p.name) && this.age == p.age && this.gender == p.gender;
            }
            //如果不是Person ,则直接返回false
            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;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    请添加图片描述
    请添加图片描述

    hashcode方法

    请添加图片描述

    package com.hspedu.object_;
    
    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 {}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    toString方法

    请添加图片描述

    package com.hspedu.object_;
    
    public class ToString_ {
        public static void main(String[] args) {
    
            /*
            Object的toString() 源码
            (1)getClass().getName() 类的全类名(包名+类名 )
            (2)Integer.toHexString(hashCode()) 将对象的hashCode值转成16进制字符串
            public String toString() {
                return getClass().getName() + "@" + Integer.toHexString(hashCode());
            }
             */
    
            Monster monster = new Monster("小妖怪", "巡山的", 1000);
            System.out.println(monster.toString() + " hashcode=" + monster.hashCode());
    
            System.out.println("==当直接输出一个对象时,toString 方法会被默认的调用==");
            System.out.println(monster); //等价 monster.toString()
    
    
    
        }
    }
    
    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;
        }
    
        //重写toString方法, 输出对象的属性
        //使用快捷键即可 alt+insert -> toString
        @Override
        public String toString() { //重写后,一般是把对象的属性值输出,当然程序员也可以自己定制
            return "Monster{" +
                    "name='" + name + '\'' +
                    ", job='" + job + '\'' +
                    ", sal=" + sal +
                    '}';
        }
    
        @Override
        protected void finalize() throws Throwable {
            System.out.println("fin..");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    finalize方法

    请添加图片描述

    package com.hspedu.object_;
    
    //演示 Finalize的用法
    public class Finalize_ {
        public static void main(String[] args) {
    
            Car bmw = new Car("宝马");
            //这时 car对象就是一个垃圾,垃圾回收器就会回收(销毁)对象, 在销毁对象前,会调用该对象的finalize方法
            //,程序员就可以在 finalize中,写自己的业务逻辑代码(比如释放资源:数据库连接,或者打开文件..)
            //,如果程序员不重写 finalize,那么就会调用 Object类的 finalize, 即默认处理
            //,如果程序员重写了finalize, 就可以实现自己的逻辑
            bmw = null;
            System.gc();//主动调用垃圾回收器
    
            System.out.println("程序退出了....");
        }
    }
    class Car {
        private String name;
        //属性, 资源。。
        public Car(String name) {
            this.name = name;
        }
        //重写finalize
        @Override
        protected void finalize() throws Throwable {
            System.out.println("我们销毁 汽车" + name );
            System.out.println("释放了某些资源...");
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    断点调试

    请添加图片描述
    请添加图片描述
    这里有许多调试的注意事项和案例看原视频

    项目-零钱通

    请添加图片描述
    请添加图片描述

    package com.hspedu.smallchange.oop;
    
    /**
     * 这里我们直接调用SmallChangeSysOOP 对象,显示主菜单即可
     */
    public class SmallChangeSysApp {
    
        public static void main(String[] args) {
            System.out.println("====hello公司====");
            new SmallChangeSysOOP().mainMenu();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    package com.hspedu.smallchange.oop;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    
    /**
     * 该类是完成零钱通的各个功能的类
     * 使用OOP(面向对象编程)
     * 将各个功能对应一个方法.
     */
    public class SmallChangeSysOOP {
    
        //属性..
        //定义相关的变量
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        String key = "";
    
        //2. 完成零钱通明细
        //老韩思路, (1) 可以把收益入账和消费,保存到数组 (2) 可以使用对象 (3) 简单的话可以使用String拼接
        String details = "-----------------零钱通明细------------------";
    
        //3. 完成收益入账  完成功能驱动程序员增加新的变化和代码
        //老韩思路, 定义新的变量
        double money = 0;
        double balance = 0;
        Date date = null; // date 是 java.util.Date 类型,表示日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //可以用于日期格式化的
    
        //4. 消费
        //定义新变量,保存消费的原因
        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 分支控制
                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();
            //money 的值范围应该校验 -》 一会在完善
            //老师思路, 编程思想
            //找出不正确的金额条件,然后给出提示, 就直接return
            if(money <= 0) {
                System.out.println("收益入账金额 需要 大于 0");
                return; //退出方法,不在执行后面的代码。
            }
            //找出正确金额的条件
            balance += money;
            //拼接收益入账信息到 details
            date = new Date(); //获取当前日期
            details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
    
        }
        //消费
        public void pay() {
            System.out.print("消费金额:");
            money = scanner.nextDouble();
            //money 的值范围应该校验 -》 一会在完善
            //找出金额不正确的情况
            //过关斩将 校验方式.
            if(money <= 0 || money > balance) {
                System.out.println("你的消费金额 应该在 0-" + balance);
                return;
            }
            System.out.print("消费说明:");
            note = scanner.next();
            balance -= money;
            //拼接消费信息到 details
            date = new Date(); //获取当前日期
            details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
        }
    
        //退出
        public void exit() {
            //用户输入4退出时,给出提示"你确定要退出吗? y/n",必须输入正确的y/n ,
            // 否则循环输入指令,直到输入y 或者 n
            // 老韩思路分析
            // (1) 定义一个变量 choice, 接收用户的输入
            // (2) 使用 while + break, 来处理接收到的输入时 y 或者 n
            // (3) 退出while后,再判断choice是y还是n ,就可以决定是否退出
            // (4) 建议一段代码,完成一个小功能,尽量不要混在一起
            String choice = "";
            while (true) { //要求用户必须输入y/n ,否则就一直循环
                System.out.println("你确定要退出吗? y/n");
                choice = scanner.next();
                if ("y".equals(choice) || "n".equals(choice)) {
                    break;
                }
                //第二个方案
    //                        if("y".equals(choice)) {
    //                            loop = false;
    //                            break;
    //                        } else if ("n".equals(choice)) {
    //                            break;
    //                        }
            }
    
            //当用户退出while ,进行判断
            if (choice.equals("y")) {
                loop = false;
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141

    本章作业

    请添加图片描述

    package com.hspedu.homework;
    
    public class Homework01 {
    
        public static void main(String[] args) {
    
            //初始化Person 对象数组,有3个person对象
            Person[] persons = new Person[3];
            persons[0] = new Person("mary",30, "PHP工程师");
            persons[1] = new Person("tom",50, "大数据工程师");
            persons[2] = new Person("smith",10, "JavaEE工程师");
    
    
    
            //输出当前对象数组
            for (int i = 0; i < persons.length; i++) {
                System.out.println(persons[i]);//默认对象的.toString()
            }
    
            //使用冒泡排序
            Person tmp = null;//临时变量,用于交换
            for(int i = 0; i < persons.length -1 ;i++) {//外层循环
                for(int j = 0; j < persons.length -1 - i; j++) {//内层循环
                    //并按照 age 从 大到 小进行排序, 如果前面的人的age < 后面人的年龄,就交换
                    //要求按照名字的长度从小到大 if(persons[i].getName().length() > persons[i+1].getName().length())
                    if(persons[j].getAge() > persons[j+1].getAge()) {
                        tmp = persons[j];
                        persons[j] = persons[j+1];
                        persons[j+1]= tmp;
                    }
    
                }
            }
    
            System.out.println("排序后的效果");
            for (int i = 0; i < persons.length; i++) {
                System.out.println(persons[i]);//默认对象的.toString()
            }
    
        }
    
        /*
        定义一个Person类 {name, age, job}, 初始化Person 对象数组,有3个person对象,
        并按照 age 从 大到 小进行排序, 提示,使用冒泡排序
         */
    }
    
    class Person {
        private String name;
        private int age;
        private String job;
    
        public Person(String name, int age, String job) {
            this.name = name;
            this.age = age;
            this.job = job;
        }
    
        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 getJob() {
            return job;
        }
    
        public void setJob(String job) {
            this.job = job;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", job='" + job + '\'' +
                    '}';
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    请添加图片描述

    package com.hspedu.homework;
    
    public class Homework08 {
        public static void main(String[] args) {
    //        CheckingAccount checkingAccount = new CheckingAccount(1000);
    //        checkingAccount.deposit(10);// 1010 - 1 = 1009
    //        checkingAccount.withdraw(9);//1009 - 9 = 1000 -1= 999
    //        System.out.println(checkingAccount.getBalance());
    
            //测试SavingsAccount
            SavingsAccount savingsAccount = new SavingsAccount(1000);
            savingsAccount.deposit(100);
            savingsAccount.deposit(100);
            savingsAccount.deposit(100);
            System.out.println(savingsAccount.getBalance());//1300
            savingsAccount.deposit(100);
            System.out.println(savingsAccount.getBalance());//1400-1=1399
    
            //月初,定时器自动调用一下 earnMonthlyInterest
            savingsAccount.earnMonthlyInterest();//统计利息
            System.out.println(savingsAccount.getBalance());//1399 + 13.99 =1412.99
            savingsAccount.withdraw(100);//免手续
            System.out.println(savingsAccount.getBalance());//1412.99 -100 =1312.99
            savingsAccount.withdraw(100);//免手续
            savingsAccount.withdraw(100);//免手续
            System.out.println(savingsAccount.getBalance());//1412.99 -200 =1112.99
            savingsAccount.deposit(100);//扣手续费
            System.out.println(savingsAccount.getBalance());//1112.99 + 100 = 1212.99 - 1 = 1211.99
    
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    package com.hspedu.homework;
    
    public class BankAccount {//父类
        private double balance ;//余额
        public BankAccount(double initialBalance) {
            this.balance = initialBalance;
        }
        //存款
        public void deposit(double amount)  {
            balance += amount;
        }
        //取款
        public void withdraw(double amount)  {
            balance -= amount;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    package com.hspedu.homework;
    
    
    /*
    在上面类的基础上扩展 新类CheckingAccount对每次存款和取款都收取1美元的手续费
     */
    public class CheckingAccount extends BankAccount{//新的账号
        //属性
        public CheckingAccount(double initialBalance) {
            super(initialBalance);
        }
    
        @Override
        public void deposit(double amount) {//存款
            super.deposit(amount - 1);//巧妙的使用了父类的 deposit
            //1 块钱转入银行的账号
        }
    
        @Override
        public void withdraw(double amount) {//取款
            super.withdraw(amount + 1);
            //1 块钱转入银行的账号
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    package com.hspedu.homework;
    
    /*
    扩展前一个练习的BankAccount类,
    新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用),
    并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数
     */
    public class SavingsAccount extends BankAccount {
    
        //老韩分析
        //新增加属性
        private int count = 3;
        private double rate = 0.01;//利率
    
        public void earnMonthlyInterest() {//每个月初,我们统计上个月的利息,同时将count=3
            count = 3;//
            super.deposit(getBalance() * rate);
        }
    
        @Override
        public void deposit(double amount) {
            //判断是否还可以免手续费
            if(count > 0) {
                super.deposit(amount);
            } else {
                super.deposit(amount - 1);//1 块转入银行
            }
            count--;//减去一次
        }
    
        @Override
        public void withdraw(double amount) {//取款
            //判断是否还可以免手续费
            if(count > 0) {
                super.withdraw(amount);
            } else {
                super.withdraw(amount + 1);//1 块转入银行
            }
            count--;
        }
    
        public SavingsAccount(double initialBalance) {
            super(initialBalance);
        }
    
    
        public int getCount() {
            return count;
        }
    
        public void setCount(int count) {
            this.count = count;
        }
    
        public double getRate() {
            return rate;
        }
    
        public void setRate(double rate) {
            this.rate = rate;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    请添加图片描述
    请添加图片描述

    最后

    附上韩顺平老师这节内容的章节总结原视频有助于知识梳理

    进度360/910 ,学习永无止境!!!

  • 相关阅读:
    yandex的目标设置参考1(针对不同类型的网站)
    Elk-Metricbeat配置Nginx的日志分析 (Metricbeat-part2)
    【笔记】:当SQL需要查询的字段过多,且存在个别字段不需要作展示,即去除指定查询字段,节省响应时间
    【Flutter系列】第一期——初识Dart语言
    在Python中将图像转换为ASCII字符图
    轻松打造自己的ChatGPT应用,AI应用,源码附赠
    【哈工大大一年度项目经验与感想】立项篇 上(2021.9.17~2021.11.17)
    C语言——结构体(位段)、联合体、枚举
    WPF中Dispatcher对象的用途是什么
    java源码系列:HashMap底层存储原理详解——2、技术本质-原理过程-数据结构
  • 原文地址:https://blog.csdn.net/m0_68516464/article/details/127135294