Java 10'var'和继承 [英] Java 10 'var' and inheritance
问题描述
在查看 var
功能后,看到这里:
我在使用JDK 10设置Eclipse / IntelliJ IDEA IDE时遇到了困难,因此我向Stack请求帮助:溢出用户一个有效的Java环境。
I have encountered difficulties with setting up my Eclipse/IntelliJ IDEA IDEs with JDK 10 and am therefore asking help from Stack Overflow users who have a working Java 10 environment.
请考虑以下事项:
public class A {
public void someMethod() { ... }
}
public class B extends A{
@Override
public void someMethod() { ... }
}
...
...
...
var myA = new A(); // Works as expected
myA = new B(); // Expected to fail in compilation due to var being
// syntactic sugar for declaring an A type
myA = (A) (new B()); // Should work
myA.someMethod(); // The question - which someMethod implementation is called?
当使用 var
时,我希望JVM识别变量所包含的派生类类型。并且在执行myA.someMethod()时执行B:someMethod()而不是A:someMethod()。
When using var
, I expect the JVM to recognize the deriving class type which the variable holds. And to execute B:someMethod() instead of A:someMethod() when executing myA.someMethod().
这是否确实如此?
推荐答案
感谢 nullpointer 提供链接到在线Java 10编译器,我得到了以下有趣的结果:
Thanks to nullpointer providing a link to an online Java 10 compiler, I got the following interesting results:
public class Main {
static class A {
public void someMethod() { System.out.println(this.getClass().getName()); }
}
static class B extends A{
@Override
public void someMethod() { System.out.println("Derived: " + this.getClass().getName()); }
}
public static void main(String[] args) {
var myA = new A();
myA.someMethod();
myA = new B(); // does not fail to compile!
myA.someMethod();
}
}
输出:
Main$A // As expected
Derived: Main$B // As expected in inheritance
结论 - var是语法糖:
var myA = new A()
是相当于 A myA = new A()
,其中包含与之关联的所有OOP。
Conclusion - var is syntactic sugar:
var myA = new A()
is equivalent to A myA = new A()
, with all the OOP associated with it.
PS:我试过与一个拥有匿名课程的var玩一点并提出这个有趣的行为 - 再次感谢 nullpointer 提及它是为什么的副本我们不能将两个推断变量作为匿名类分配给彼此:
PS: I tried to play a little with a var holding an anonymous class and came up with this interesting behavior - thanks (yet again) to nullpointer for mentioning it as a duplicate of why can't we assign two inferred variables as an anonymous class to each other:
static interface Inter {
public void method();
}
public static void main(String[] args) {
var inter = new Inter() {
@Override
public void method() {System.out.println("popo");}
};
inter.method();
inter = new Inter() {
@Override
public void method() {System.out.println("koko");}
};
inter.method();
}
输出:
Main.java:11: error: incompatible types: <anonymous Inter> cannot be converted to <anonymous Inter>
inter = new Inter() {
^
var的第二个赋值由于第二个匿名类类型与第一个匿名类类型不同而失败 - 强制执行 var
关键字的语法糖角色。
The second assignment to var fails due to the second anonymous class type differing from the first anonymous class type - enforcing the syntactic sugar role of the var
keyword.
令人惊讶的是错误信息不再精确 - 目前没有意义,因为错误中显示的类型名称是相同的!
It is surprising that the error message is not more refined - currently it makes little sense since the type names displayed in the error are identical!
这篇关于Java 10'var'和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!