简体   繁体   English

Java中的子类编译

[英]Subclass compilation in Java

我已经了解到继承是一种“编译时”现象。在另一个地方,我也了解到超类代码是由类加载器加载的,我推断这是在运行时发生的。这使我对类的本质有些困惑。 sublass的类文件是否包含超类的实际编译代码,还是在运行时进行访问?

So consider you create a class that inherits a class that is included in a 3rd party jar file. 因此,请考虑创建一个继承第3方jar文件中包含的类的类。

In order to compile your code you need to have the 3rd party jar file in the classpath of your compiler. 为了编译您的代码,您需要在编译器的类路径中包含第3方jar文件。

In order to run your code you will also need the jar file in the classpath of the java command that launches the application. 为了运行您的代码,您还将需要启动应用程序的java命令的类路径中的jar文件。

Your subclass does not contain the code of the superclass, it is in the jar files. 您的子类不包含超类的代码,而是在jar文件中。 Your compiled class contains a reference to the superclass. 您的已编译类包含对超类的引用。 When your class is loaded by the classloader it searches the classpath for the superclass and loads it. 当类加载器加载您的类时,它将在类路径中搜索超类并将其加载。

Where did you read it's compile time? 您在哪里读到了编译时间? I guess if you're compiling your subclass then yes, it needs to have a superclass to reference when being compiled. 我想如果您正在编译子类,那么是的,它在编译时需要有一个超类来引用。

But when you actually run the code it is dynamically linked as per: 但是,当您实际运行代码时,它会按照以下方式动态链接:

http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html

"The Java Virtual Machine dynamically loads, links and initializes classes and interfaces" “ Java虚拟机动态加载,链接和初始化类和接口”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM