目录
8.3 方法调用
8.3.1 解析
代码清单8-5, 方法静态解析演示
/*
* 代码清单8-5, 方法静态解析演示
* @author:huang
*/
package cn.chapter8;
public class StaticResolution {
public static void sayHello(){
System.out.println("hello world");
}
public static void main(String[] args) {
StaticResolution.sayHello();
}
}
运行结果:
hello world
使用javap命令查看这段程序的字节码,会发现的确是通过invokestatic命令来dial用sayHello()方法的
(javap的使用,点击这里)
Microsoft Windows [版本 10.0.17763.316]
(c) 2018 Microsoft Corporation。保留所有权利。
C:\Users\ee>cd Desktop/Java_VM/bin/cn/chapter8/
C:\Users\ee\Desktop\Java_VM\bin\cn\chapter8>javap -verbose StaticResolution
。。。。。。(中间的我删了,太多了,这里也用不到)
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=0, locals=1, args_size=1
0: invokestatic #31 // Method sayHello:()V
3: return
LineNumberTable:
line 16: 0
line 18: 3
LocalVariableTable:
Start Length Slot Name Signature
0 4 0 args [Ljava/lang/String;
}
8.3.2 分派
1 静态分派
代码清单8-5, 方法静态分派演示
/*
* 代码清单8-5, 方法静态分派演示
* @author:huang
*/
package cn.chapter8;
public class StaticDispatch {
static abstract class Human{
}
static class Man extends Human{
}
static class Women 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(Women guy){
System.out.println("hello, lady!");
}
public static void main(String[] args) {
Human man = new Man();
Human women = new Women();
StaticDispatch sr = new StaticDispatch();
sr.sayHello(man);
sr.sayHello(women);
}
}
运行结果:(要是面试遇到这种题目,我就挂了)
hello, guy!
hello, guy!
代码清单8-7, 重载方法匹配优先级
/*
* 代码清单8-7, 重载方法匹配优先级
* @author:huang
*/
package cn.chapter8;
import java.io.Serializable;
public class Overload {
public static void sayHello(Object arg){
System.out.println("hello Object");
}
public static void sayHello(int arg){
System.out.println("hello int");
}
public static void sayHello(long arg){
System.out.println("hello long");
}
public static void sayHello(Character arg){
System.out.println("hello Character");
}
public static void sayHello(char arg){
System.out.println("hello char");
}
public static void sayHello(char... arg){
System.out.println("hello char...");
}
public static void sayHello(Serializable arg){
System.out.println("hello Serializable");
}
public static void main(String[] args) {
sayHello('a');
}
}
上面代码运行输出:
hello char
2 动态分派
代码清单8-8, 方法动态分派演示
/*
* 代码清单8-8, 方法动态分派演示
* @author:huang
*/
package cn.chapter8;
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 Women extends Human{
@Override
protected void sayHello(){
System.out.println("women say hello");
}
}
public static void main(String[] args) {
Human man = new Man();
Human women = new Women();
man.sayHello();
women.sayHello();
man = new Women();
man.sayHello();
}
}
运行结果:(面试题遇到这种,我肯定挂)
man say hello
women say hello
women say hello
代码清单8-9,main()方法的字节码
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=1
0: new #16 // class cn/chapter8/DynamicDispatch$Man
3: dup
4: invokespecial #18 // Method cn/chapter8/DynamicDispatch$Man."<init>":()V
7: astore_1
8: new #19 // class cn/chapter8/DynamicDispatch$Women
11: dup
12: invokespecial #21 // Method cn/chapter8/DynamicDispatch$Women."<init>":()V
15: astore_2
16: aload_1
17: invokevirtual #22 // Method cn/chapter8/DynamicDispatch$Human.sayHello:()V
20: aload_2
21: invokevirtual #22 // Method cn/chapter8/DynamicDispatch$Human.sayHello:()V
24: new #19 // class cn/chapter8/DynamicDispatch$Women
27: dup
28: invokespecial #21 // Method cn/chapter8/DynamicDispatch$Women."<init>":()V
31: astore_1
32: aload_1
33: invokevirtual #22 // Method cn/chapter8/DynamicDispatch$Human.sayHello:()V
36: return
LineNumberTable:
line 28: 0
line 29: 8
line 30: 16
line 31: 20
line 32: 24
line 33: 32
line 34: 36
LocalVariableTable:
Start Length Slot Name Signature
0 37 0 args [Ljava/lang/String;
8 29 1 man Lcn/chapter8/DynamicDispatch$Human;
16 21 2 women Lcn/chapter8/DynamicDispatch$Human;
}
3 单分派与多分派
代码清单8-10, 单分派和多分派演示
/*
* 代码清单8-10, 单分派和多分派演示
* @author:huang
*/
package cn.chapter8;
public class Dispatch {
static class QQ{}
static class _360{}
public static class Father{
public void hardChoice(QQ arg){
System.out.println("father choose qq");
}
public void hardChoice(_360 arg){
System.out.println("father choose _360");
}
}
public static class Son extends Father{
public void hardChoice(QQ arg){
System.out.println("son choose qq");
}
public void hardChoice(_360 arg){
System.out.println("son choose _360");
}
}
public static void main(String[] args) {
Father father = new Father();
Father son = new Son();
father.hardChoice(new _360());
son.hardChoice(new QQ());
}
}
运行结果:
father choose _360
son choose qq