Java casting order

前端 未结 2 721
不知归路
不知归路 2020-12-10 12:54

Let\'s say I have the following setup

class A {
    B foo();
}

class C extends B {

}

// later
A a = new A();
C theFoo = (C)a.foo();

We k

2条回答
  •  鱼传尺愫
    2020-12-10 13:29

    Method call has a higher operator precedence than type casting, so (C) a.foo() will first call a.foo() and cast the result to type C. In contrast, ((C) a).foo() first casts a to type C and then calls its foo() method.

提交回复
热议问题