java解释器和编译器规则冲突
我有一个“理论”问题,但这是为了理解java解释器和编译器规则冲突,java,compilation,compiler-errors,syntax-error,Java,Compilation,Compiler Errors,Syntax Error,我有一个“理论”问题,但这是为了理解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 Lis
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
}
class A{}
类B扩展了{}
X类{
公共B getValue(){return null;}
}
类Y扩展X{
public A getValue(){return null;}//此处存在编译错误
}
Z类{
public List getAList(List x){return x;}//此处编译错误
}
注意:我知道哪些语法错误以及如何修复它们
我的问题是:
如果您理解第一个问题的答案,您可以单独回答第二个问题;) 我想是不明确的行为。所有的事情都不能再正常工作了(多态性、虚拟方法调度等),这就是为什么要确定一组规则,让代码遵循这些规则(JLS)。没有人会费心去找出“忽略”这些编译错误所导致的确切问题,因为这完全是理论上的。因此:未定义。@Jeroenvanevel-“未定义的行为”不是我想要的答案。我编辑这个问题是为了让它更清楚。它可能不是你想要的,但我怀疑你会得到任何其他东西。你所问的行为实际上没有定义,所以你只能猜测这个假设情景。也许执行了错误的方法?也许运行时开始抛出错误?谁知道呢。@Jeroenvanevel-我相信这与内存、解释、java语法/方法评估、操作系统寄存器等有关。对此有一个恰当的解释。我只是(还)找不到它@ZivLevy我有几个问题要问你。你知道动态方法调度是什么吗?你知道协方差是什么回报类型吗?您认为这些功能在不设置您幻想中要删除的编译限制的情况下是可能的吗?如果我最后一个问题的答案是否定的,那么你有你的答案。
What could have happen in runtime if we would "ignore" (in theory) the compiler errors?
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}