更新时间:2023-10-22 08:31:10
这是因为语法。运算符的优先级恰好在相同的词汇序列可以用两种不同的方式解析时发挥作用但事实并非如此。
This is because of how the grammar of Java language is defined. Precedence of operators comes into play just when the same lexical sequence could be parsed in two different ways but this is not the case.
为什么?
因为分配定义如下:
Primary:
...
new Creator
方法调用定义如下:
Selector:
. Identifier [Arguments]
...
并且都在这里使用:
Expression3:
...
Primary { Selector } { PostfixOp }
所以会发生的是
new myClass().myFunction();
被解析为
Expression
|
|
---------+--------
| |
| |
Primary Selector
| |
| |
---+--- ...
| |
new Creator
所以根据优先级没有选择,因为主
之前减少了。请注意,对于特殊情况,例如
So there is no choice according to priority because the Primary
is reduced before. Mind that for the special situation like
new OuterClass.InnerClass()
类名实际上是在 new
运算符之前解析的,并且确实有处理该情况的规则。如果你想看到它们,请检查语法。
the class name is actually parsed before the new
operator and there are rules to handle that case indeed. Check the grammar if you like to see them.