虚方法表与动态分派机制

1、创建MyTest7

package com.example.jvm.bytecode;

import java.util.Date;

public class MyTest7 {

    public static void main(String[] args) {
        Animal animal = new Animal();
        Animal dog = new Dog();
        animal.test("hello");
        dog.test(new Date());

    }
}

//既有方法的重载,又有Dog子类对方法的重写
class Animal{

    public void test(String str){
        System.out.println("animal test");
    }

    public void test(Date date){
        System.out.println("animal date");
    }
}

class Dog extends Animal{

    @Override
    public void test(String str) {
        System.out.println("dog test");
    }

    @Override
    public void test(Date date) {
        System.out.println("dog date");
    }

}

  输出结果:

animal test
dog date

猜你喜欢

转载自www.cnblogs.com/linlf03/p/11108677.html