0036 Java动态绑定机制、多态应用

 

 

 

 

 

 

 

 

 

 

/*
    Java的动态绑定机制
    1.当调用对象方法时,该方法会和该对象的运行类型(内存地址)绑定
    2.当调用对象属性时,没有动态绑定机制,谁声明谁调用
 */
//演示 动态绑定机制
public class DynamicBinding {
    public static void main(String[] args) {

        A a = new B();  //编译类型A,运行类型B
        System.out.println(a.sum());    //30
        System.out.println(a.sum1());   //20
    }
}
class A{    //父类
    public int i = 10;
    public int sum(){
        return getI() + 10; //运行类型为B,调用子类getI(),20+10=30
    }
    public int sum1(){
        return i + 10;  //属性没有动态绑定,10+10=20
    }
    public int getI(){
        return i;
    }
}
class B extends  A{ //子类
    public int i = 20;

    public int getI(){
        return i;
    }
}
/*  多态应用1:多态数组
    有一个继承结构如下:要求创建一个Person对象、两个Student对象和两个Teacher对象,统一放在数组中,并调用say方法
    调用子类特有的方法,如Teacher有一个teach,Student有一个study
 */

public class Polymorphic_Apply {
    public static void main(String[] args) {

        Person[] persons = new Person[5];
        persons[0] = new Person("huang",20);
        persons[1] = new Student("rui",20,66);
        persons[2] = new Student("rui",22,99);
        persons[3] = new Teacher("smith",40,10000);
        persons[4] = new Teacher("smith",44,11111);
        //循环遍历多态数组,调用say
        for (int i = 0; i < persons.length ; i++) {
            System.out.println(persons[i].say());//编译类型是person,运行类型(Person,Student,Teacher)
            //使用if instanceof强转
            if(persons[i] instanceof Student){
               // ((Student) persons[i]).study();
                Student student = (Student) persons[i];
                student.study();
            }else if(persons[i] instanceof Teacher){
                ((Teacher)persons[i]).teach();
            }else if(persons[i] instanceof Person){

            }else{
                System.out.println("输入类型有误");
            }

        }
    }
}

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;
    }
}

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;
    }

    public String say(){
        return super.say() + " score = " + score;
    }
    //特有方法
    public void study(){
        System.out.println("学生特有方法被调用");
    }
}

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("老师的特有方法被调用");
    }
}
/*
    多态应用2:多态参数
    方法定义的形参类型为父类类型,实参类型允许为子类类型

    1.定义员工类Employee,包含姓名和工资(private),以及计算年工资getAnnual的方法。普通员工和经理继承了员工,
    经理类多了奖金bonus属性和管理manage方法,普通员工类多了work方法,要求分别重写getAnnual方法

    2.测试类中添加一个方法showEmpAnnal(Employee e),实现获取任何员工对象的年工资,并在main方法中调用该方法

    3.测试类中添加一个方法testWork,如果是普通员工,则调用work方法,如果是经理,则调用manage方法
 */
public class Ploymorphic_Apply2 {
    public static void main(String[] args) {
        Worker huang = new Worker("huang", 20000);
        Manager rui = new Manager("rui", 20000, 10000);
        Ploymorphic_Apply2 ploymorphic_apply2 = new Ploymorphic_Apply2();
        ploymorphic_apply2.showEmpAnnal(huang);
        ploymorphic_apply2.showEmpAnnal(rui);

        ploymorphic_apply2.testWork(huang);
        ploymorphic_apply2.testWork(rui);
    }
    private void showEmpAnnal(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("不做处理");
        }
    }
}

class Employee{
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = 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;
    }

    public double getAnnual(){
        return salary * 12;
    }
}

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

    public void work(){
        System.out.println("普通员工work方法被调用");
    }

    @Override
    public double getAnnual() {
        return super.getAnnual();
    }
}

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("经理manage方法被调用");
    }

    @Override
    public double getAnnual() {
        return super.getAnnual() + bonus;
    }
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nzmzmc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值