一、方法调用

方法调用的唯一目的:确定要调用哪一个方法

方法调用分为解析调用和分派调用

二、非虚方法与虚方法

非虚方法: 静态方法,私有方法,父类中的方法,被final修饰的方法,实例构造器

与之对应不是非虚方法的就是虚方法了

它们都没有重写出其他版本的方法,非常适合在类加载阶段就进行解析(符号引用->直接引用)

三、调用指令

普通调用指令

  • invokestatic:调用静态方法
  • invokespecial:调用私有方法,父类中的方法,实例构造器方法,final方法
  • invokeinterface:调用接口方法
  • invokevirtual: 调用虚方法

使用invokestaticinvokespecial指令的一定是非虚方法

使用invokeinterface指令一定是虚方法(因为接口方法需要具体的实现类去实现)

使用invokevirtual指令的是虚方法

动态调用指令

invokedynamic: 动态解析出需要调用的方法再执行

jdk 7 出现invokedynamic,支持动态语言

测试虚方法代码

父类

public class father {
    public static void staticmethod(){
        system.out.println("father static method");
    }

    public final void finalmethod(){
        system.out.println("father final method");
    }

    public father() {
        system.out.println("father init method");
    }

    public void overridemethod(){
        system.out.println("father override method");
    }
}

接口

public interface testinterfacemethod {
    void testinterfacemethod();
}

子类

public class son extends father{

    public son() {
        //invokespecial 调用父类init 非虚方法
        super();
        //invokestatic 调用父类静态方法 非虚方法
        staticmethod();
        //invokespecial 调用子类私有方法 特殊的非虚方法
        privatemethod();
        //invokevirtual 调用子类的重写方法 虚方法
        overridemethod();
        //invokespecial 调用父类方法 非虚方法
        super.overridemethod();
        //invokespecial 调用父类final方法 非虚方法
        super.finalmethod();
        //invokedynamic 动态生成接口的实现类 动态调用
        testinterfacemethod test = ()->{
            system.out.println("testinterfacemethod");
        };
        //invokeinterface 调用接口方法 虚方法
        test.testinterfacemethod();
    }

    @override
    public void overridemethod(){
        system.out.println("son override method");
    }

    private void privatemethod(){
        system.out.println("son private method");
    }

    public static void main(string[] args) {
        new son();
    }
}

注意: 接口中的默认方法也是invokeinterface,接口中的静态方法是invokestatic

四、解析调用

在编译就确定了要调用哪个方法,运行时不可以改变

解析调用 调用的是 非虚方法

五、分派调用

分派又分为静态分派与动态分配

早期绑定:解析调用和静态分派这种编译期间可以确定调用哪个方法

晚期绑定: 动态分派这种编译期无法确定,要到运行时才能确定调用哪个方法

六、静态分派

//  静态类型      	 实际类型
	list list = new arraylist();

静态分派: 根据静态类型决定方法执行的版本的分派

发生在编译期,特殊的解析调用

典型的表现就是方法的重载

public class staticdispatch {
    public void test(list list){
        system.out.println("list");
    }

    public void test(arraylist arraylist){
        system.out.println("arraylist");
    }

    public static void main(string[] args) {
        arraylist arraylist = new arraylist();
        list list = new arraylist();
        staticdispatch staticdispatch = new staticdispatch();
        staticdispatch.test(list);
        staticdispatch.test(arraylist);
    }
}
/*
list
arraylist
*/

方法的版本并不是唯一的,往往只能确定一个最适合的版本

七、动态分派

动态分派:动态期根据实际类型确定方法执行版本的分派

动态分派与重写有着紧密的联系

public class dynamicdispatch {
    public static void main(string[] args) {
        father father = new father();
        father son = new son();

        father.hello();
        son.hello();
    }
    static class father{
        public void hello(){
            system.out.println("father hello");
        }
    }

    static class son extends father{
        @override
        public void hello() {
            system.out.println("son hello");
        }
    }
}
/*
father hello
son hello
*/

虽然常量池中的符号引用相同,invokevirtual指令最终指向的方法却不一样

分析invokevirtual指令搞懂它是如何确定调用的方法

1.invokevirtual找到栈顶元素的实际类型

2.如果在这个实际类型中找到与常量池中描述符与简单名称相符的方法,并通过访问权限的验证就返回这个方法的引用(未通过权限验证返回illegalaccessexception非法访问异常)

3.如果在实际类型中未找到,就去实际类型的父类中寻找(没找到抛出abstractmethoderror异常)

因此,子类重写父类方法时,根据invokevirtual指令规则,先找实际类型,所以才存在重写的多态

频繁的动态分派会重新查找栈顶元素实际类型,会影响执行效率

为提高性能,jvm在该类方法区建立虚方法表使用索引表来代替查找

字段不存在多态

当子类出现与父类相同的字段,子类会覆盖父类的字段

public class dynamicdispatch {
    public static void main(string[] args) {
        father son = new son();
    }
    static class father{
        int num = 1;

        public father() {
            hello();
        }

        public void hello(){
            system.out.println("father hello " + num);
        }
    }

    static class son extends father{
        int num = 2;

        public son() {
            hello();
        }

        @override
        public void hello() {
            system.out.println("son hello "+ num);
        }
    }
}
/*
son hello 0
son hello 2
*/

先对父类进行初始化,所以会先执行父类中的构造方法,而构造方法去执行了hello()方法,此时的实际类型是son于是会去执行son的hello方法,此时子类还未初始化成员变量,只是有个默认值,所以输出son hello 0

八、单分派与多分派

public class dynamicdispatch {
    public static void main(string[] args) {
        father son = new son();
        father father = new father();

        son.hello(new nod());
        father.hello(new wave());
    }
    static class father{


        public void hello(nod nod){
            system.out.println("father nod hello " );
        }

        public void hello(wave wave){
            system.out.println("father wave hello " );
        }
    }

    static class son extends father{

        @override
        public void hello(nod nod) {
            system.out.println("son nod hello");
        }

        @override
        public void hello(wave wave) {
            system.out.println("son wave hello");
        }
    }

    //招手
    static class wave{}
    //点头
    static class nod{}
}
/*
son nod hello
father wave hello 
*/

宗量: 方法参数与方法调用者

分派还可以分为单,多分派

单分派:根据一个宗量选择方法

多分派:根据多个宗量选择方法

在编译时,不仅要关心静态类型是father还是son,还要关心参数是nod还是wave,所以静态分派是多分派(根据两个宗量对方法进行选择)

在执行son.hello(new nod())时只需要关心实际类型是son还是father,所以动态分派是单分派(根据一个宗量对方法进行选择)

到此这篇关于java如何对方法进行调用详解的文章就介绍到这了,更多相关java调用方法内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!