问题描述
为什么必须始终在类声明中扩展后编写实现,例如:
Why implements must always be written after extends in class declaration for example:
public class Register extends ActionSupport implements ModelDriven
为什么不
public class Register implements ModelDriven extends ActionSupport
是编译时错误
推荐答案
当Java编译器将类转换为字节码时,它必须首先查看父类。这是因为类的底层实现是指向父类的字节码 - 它包含相关的方法和字段。然后它添加指向子类函数的代码的指针 - 其中一些是由'implements'关键字强制执行的。
When the Java compiler turns a class into bytecode, it must first look to a parent class. That is because the underlying implementation of classes is to point to the bytecode of the parent class - which holds the relevant methods and fields. Then it adds in pointers to the code of the child class functions - some of which are mandated by the 'implements' keyword.
因为父类必须是可编译的,所以如果编译器预先知道该类是什么,则更容易。此外,您只能扩展一个类,但可以实现任意数量的接口。如果extends关键字可以在任意数量的实现指令之间混合,则编译时间会增加。编译器希望尽可能快地失败以减少开发时间,因此这种选择是合乎逻辑的。此外,它可以帮助您以同样的理由清楚地思考课程。
Because the parent class must be compilable, it is easier if the compiler knows up front what that class is. Further, you can extend only one class but implement any number of interfaces. The compilation time climbs if the extends keyword can be intermingled amongst any number of implements instructions. Compilers want to fail as fast as possible to decrease dev time, so this choice is logical. Further, it helps you think clearly about the class for the same reason.
这篇关于为何“延伸”先于“实施”在课堂申报中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!