博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JVM方法分派
阅读量:4217 次
发布时间:2019-05-26

本文共 1470 字,大约阅读时间需要 4 分钟。

方法静态分派演示

/** * 方法静态分派演示 * Created by tiantao on 15-2-9. */public class StaticDispatch {    static abstract class Human {    }    static class Man extends Human {    }    static class Woman extends Human {    }    public void sayHello(Human guy) {        System.out.println("hello,guy!");    }    public void sayHello(Man guy) {        System.out.println("hello,gentleman!");    }    public void sayHello(Woman guy) {        System.out.println("hello,lady!");    }    public static void main(String[] args) {        Human man = new Man();        Human woman = new Woman();        StaticDispatch testJVM = new StaticDispatch();        testJVM.sayHello(man);        testJVM.sayHello(woman);    }}
执行输出:

hello,guy!

hello,guy!

接下来是方法动态分派演示

/** * 方法动态分派演示 * Created by tiantao on 15-2-9. */public class DynamicDispatch {    static abstract class Human {        protected abstract void sayHello();    }    static class Man extends Human {        @Override        protected void sayHello() {            System.out.println("man say hello!");        }    }    static class Woman extends Human {        @Override        protected void sayHello() {            System.out.println("woman say hello!");        }    }    public static void main(String[] args) {        Human man = new Man();        Human woman = new Woman();        man.sayHello();        woman.sayHello();    }}
执行输出:

man say hello!

woman say hello!

本来对方法重载和方法得重写概念挺清晰得,今天看了书里的这两个例子一下被带模糊了。

对第一个例子,静态分派的结果有点小意外。加强一下记忆。

转载地址:http://ionmi.baihongyu.com/

你可能感兴趣的文章
js函数中传入的event参数
查看>>
[hive]优化策略
查看>>
c++14现代内存管理
查看>>
右值引用,move语义和完美转发
查看>>
c++使用宏检测类是否包含某个函数或者变量属性
查看>>
CSS之Multi-columns的column-gap和column-rule
查看>>
CSS之Multi-columns的跨列
查看>>
CSS之浮动(一)
查看>>
CSS之浮动(二)
查看>>
AtomicInteger源码解析
查看>>
CopyOnWriteArraySet源码学习
查看>>
Openfiler 配置 NFS 示例
查看>>
Oracle 11.2.0.1 RAC GRID 无法启动 : Oracle High Availability Services startup failed
查看>>
Oracle 18c 单实例安装手册 详细截图版
查看>>
Oracle Linux 6.1 + Oracle 11.2.0.1 RAC + RAW 安装文档
查看>>
Oracle 11g 新特性 -- Online Patching (Hot Patching 热补丁)说明
查看>>
Oracle 11g 新特性 -- ASM 增强 说明
查看>>
Oracle 11g 新特性 -- Database Replay (重演) 说明
查看>>
Oracle 11g 新特性 -- 自动诊断资料档案库(ADR) 说明
查看>>
Oracle 11g 新特性 -- RMAN Data Recovery Advisor(DRA) 说明
查看>>