我有一个“理论”问题,但这是为了理解java编译规则背后的一些东西(或者解释)。
假设我们有以下代码:
class A {}
class B extends A {}
class X {
public B getValue(){return null;}
}
class Y extends X {
public A getValue(){return null;} //compilation error here
}
class Z {
public List<A> getAList(List<B> x) {return x;} //compilation error here
}备注:我知道语法错误是什么以及如何修复它们。
我的问题是:
发布于 2015-04-12 14:31:11
What could have happen in runtime if we would "ignore" (in theory) the compiler errors?编译器将Java代码转换为Byte代码,这只是一组指令。要做到这一点,它确实需要非常具体的规则来知道该做什么-像关键字,括号-语法。
如果语法错误,它不知道如何处理它,因此不能转换它。
对于编译器来说,尝试用这样的随机字符序列制作程序和你认为“几乎是程序,但语法错误很少”的东西并没有太大的区别。
发布于 2015-04-12 14:34:42
class Y extends X {
@Override //class Y extends class X which has a public method B getValue()
public B getValue() {
return super.getValue();
}
private A getValue() {return null}X有一个名为getValue() Y扩展X的方法,因此Y具有与他的超类相同的getValue()方法。那么编译器如何知道您想要使用的getValue()方法中的哪一个,如果它们的名称相同呢?
如果你理解第一个问题的答案,你可以单独回答你的第二个问题;)
https://stackoverflow.com/questions/29590255
复制相似问题