java多态后期绑定规则 [英] java polymorphism late binding rules

查看:126
本文介绍了java多态后期绑定规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行一些多态性练习,但我无法弄清楚这种多态性是如何工作的.我没有找到有关这种锻炼的任何深入信息.我希望你们能给我一些解释.

I am trying to do some polymorphism exercises and I cannot figure out how this kind of polymorphism works. I did not find any deep information about this kind of exercised. I hope you guys could give me some explanation.

练习1:

class Top {
    public void m( Middle p ) System.out.print("A ");
}

class Middle extends Top {
    public void m( Object p ) System.out.print("M ");
    public void m( Middle p ) System.out.print("L ");
}

class Bottom extends Middle {
    public void m( Object p ) System.out.print("V ");
    public void m( Middle p ) System.out.print("X ");
}

class Test {
    public static void run() {
        Top tm = new Middle();
        Middle mb = new Bottom();

        tm.m (mb);            -> L
        tm.m(new Bottom());   -> L   why?
        mb.m(mb);             -> X
        mb.m(tm);             -> V   why?
        mb.m(new Middle());   -> X
        new Bottom().m(tm);   -> V
    }
}

练习2:

class Top {
    public void gg( Top o ) System.out.print("A ");
    public void gg( Middle m ) System.out.print("B ");
}

class Middle extends Top {
    public void gg( Top o ) System.out.print("L ");
    public void gg( Bottom b ) System.out.print("M ");
}

class Bottom extends Middle {
    public void gg( Top o ) System.out.print("X ");
    public void gg( Middle m) System.out.print("Z ");
}

class Test {
    public static void run() {
        Top oo = new Top();
        Top ff = new Middle();
        Bottom uu = new Bottom();

        oo.gg(ff);      -> A
        oo.gg(uu);      -> A   why?
        ff.gg(ff);      -> L
        ff.gg(uu);      -> B   why?
        uu.gg(ff);      -> X
        uu.gg(uu);      -> X   why?
    }
}

提前谢谢!

礼物

推荐答案

在所有这些情况下,可以考虑使用的方法取决于变量的编译时类型,但是该方法实际被称为 的时间取决于对象的运行时类型.所以

In all of these cases, the methods that can be considered depend on the compile-time type of the variable, but the method that is actually called depends on the runtime type of the object. So for

Top ff = new Middle();

Middle的方法将被调用-但它们可能是从Top继承的,并且我们只能在编译时调用Top中可用的方法,因为ff被声明为a Top.

the methods of Middle are the ones that will be called - but these may be inherited from Top, and we can only call methods that are available in Top at compile time, because ff is declared as a Top.

要确定调用哪个重载方法,我们查看参数类型,然后选择最具体的方法.因此,如果我们必须在以下选项之间进行选择:

To determine which of the overloaded methods is called, we look at the parameter type, and choose the most specific method. So if we have to choose between:

public void m( Object p ) System.out.print("M ");
public void m( Middle p ) System.out.print("L ");

,而我们传递的是Bottom,则将选择第二种方法.您可以认为Bottom在类层次结构中比Object更接近Middle:

and we are passing a Bottom, then the second method will be chosen. You can think of Bottom as being closer to Middle than Object in the class hierarchy:

Bottom -> Middle -> Top -> Object

最后,您的某些答案是错误的(练习2)-我建议您尝试运行代码,这可能需要一些调整才能真正编译.

Finally, some of your answers are just wrong (exercise 2) - I suggest you try running the code, which may need a bit of tweaking so it actually compiles.

oo.gg(uu); // -> A why?  -- actually produces B
uu.gg(uu); // -> X why?  -- actually produces M

这篇关于java多态后期绑定规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆