Java基础 6.18

1.Java的动态绑定机制(非常重要)

当调用对象方法的时候,该方法会和对象的内存地址/运行类型绑定

当调用对象属性时,没有动态绑定机制,哪里声明,哪里使用

package com.logic.poly_.dynamic_;

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 sum() {
        return i + 20;
    }
    public int getI() {
        return i;
    }
    public int sum1() {
        return i + 10;
    }
}

2.多态的应用

多态数组

数组的定义类型为父类类型,里面保存的实际元素类型为子类类型

应用实例

package com.logic.poly_.polyarr_;

public class PolyArray {
    public static void main(String[] args) {
        //应用实例:现有一个继承结构如下 要求创建1个Person对象
        //2个Student对象和2个Teacher对象 统一放在数组里 并调用每个对象的say方法
        Person[] persons = new Person[5];
        persons[0] = new Person("jack", 19);
        persons[1] = new Student("logic", 20, 100);
        persons[2] = new Student("scott", 22, 90);
        persons[3] = new Teacher("dandan", 32, 20000);
        persons[4] = new Teacher("fuming", 30, 19000);

        //循环遍历多态数组 调用say
        for (int i = 0; i < persons.length; i++) {
            //person[i]编译类型是Person 运行类型是根据实际情况由JVM来判断
            System.out.println(persons[i].say());//动态绑定机制
        }
    }
}
package com.logic.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;
    }

}
package com.logic.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;
    }
}
package com.logic.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;
    }
}

应用实例升级

如何调用子类特有的方法,比如Teacher有一个teach,St有一个study怎么调用

package com.logic.poly_.polyarr_;

public class PolyArray {
    public static void main(String[] args) {
        //应用实例:现有一个继承结构如下 要求创建1个Person对象
        //2个Student对象和2个Teacher对象 统一放在数组里 并调用每个对象的say方法
        Person[] persons = new Person[5];
        persons[0] = new Person("jack", 19);
        persons[1] = new Student("logic", 20, 100);
        persons[2] = new Student("scott", 22, 90);
        persons[3] = new Teacher("dandan", 32, 20000);
        persons[4] = new Teacher("fuming", 30, 19000);

        //循环遍历多态数组 调用say
        for (int i = 0; i < persons.length; i++) {
            //person[i]编译类型是Person 运行类型是根据实际情况由JVM来判断
            System.out.println(persons[i].say());//动态绑定机制
            //改 判断语句 + 向下转型
            if (persons[i] instanceof Student) {//判断persons[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) {

            } else {
                System.out.println("你的类型有误,请检查...");
            }
        }
    }
}

多态参数

方法定义的形参类型为父类类型,实参类型允许为子类类型

package com.logic.poly_.polyparameter_;

/*
定义员工类Employee,包含姓名和月工资[private],以及计算年工资getAnnual的方法。
普通员工和经理继承了员工,经理类多了奖金bonus属性和管理manage方法,
普通员工类多了work方法,普通员工和经理类要求分别重写getAnnual方法
测试类中添加一个方法showEmpAnnual(Employee e),实现获取任何员工对象的年工资,
并在main方法中调用该方法 [e.getAnnual()]
测试类中添加一个方法testWork,如果是普通员工,则调用work方法,
如果是经理,则调用manage方法
 */
public class PolyParameter {
    public static void main(String[] args) {
        Worker jack = new Worker(2000, 1, "jack");
        Manager milan = new Manager(7000, 1, "milan", 50000);
        PolyParameter polyParameter = new PolyParameter();
        polyParameter.showEmpAnnual(jack);
        polyParameter.showEmpAnnual(milan);
        polyParameter.testWork(milan);
        polyParameter.testWork(jack);
    }

    //showEmpAnnual(Employee e)
    //实现获取任何员工对象的年工资
    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("不处理...");
        }
    }
}
package com.logic.poly_.polyparameter_;

public class Employee {
    private String name;
    private double salary;
    private double year;

    public Employee(double salary, double year, String name) {
        this.salary = salary;
        this.year = year;
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getYear() {
        return year;
    }

    public void setYear(double year) {
        this.year = year;
    }

    //得到年工资的方法
    public double getAnnual() {
        return salary * year * 12;
    }
}
package com.logic.poly_.polyparameter_;

public class Manager extends Employee {
    private double bonus;

    public Manager(double salary, double year, String name, double bonus) {
        super(salary, year, name);
        this.bonus = bonus;
    }

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    public void manage() {
        System.out.println("经理 " + getName() + " 正在管理...");
    }

    //重写获取年薪的方法

    @Override
    public double getAnnual() {
        return super.getAnnual() + bonus;
    }
}
package com.logic.poly_.polyparameter_;

public class Worker extends Employee {
    public Worker(double salary, double year, String name) {
        super(salary, year, name);
    }

    public void work() {
        System.out.println("员工 " + getName() + " 正在工作...");
    }

    @Override
    public double getAnnual() {//因为普通员工没有其他收入,则直接调用父类方法
        return super.getAnnual();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值