zoukankan      html  css  js  c++  java
  • 虚方法表与动态分派机制

    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

    针对于方法调用动态分派的过程,虚拟机会在类的方法区建立一个虚方法表的数据结构(virtual method table, vtable),
    针对于invokeinterface指令来说,虚拟机会建立一个叫做接口方法表的数据结构(interface method table, itable)

  • 相关阅读:
    appium在Mac上环境搭建
    判断元素的16中方法expected_conditions
    python3条件表达式和字符串
    webdriver的API
    什么是web接口
    python2函数
    python1变量,表达式和语句
    XPath学习
    接口相关概念
    解决jdk1.7,1.8共存问题小思
  • 原文地址:https://www.cnblogs.com/linlf03/p/11108677.html
Copyright © 2011-2022 走看看