gpt4 book ai didi

java - 有人可以提供 javac 的 -implicit 选项如何工作的示例吗?

转载 作者:搜寻专家 更新时间:2023-10-31 20:02:28 24 4
gpt4 key购买 nike

在 shell 命令提示符下,如果我输入 javac -help,它会告诉我 -implicit 选项指定“...是否生成类隐式引用文件的文件”。 -implicit 有两个选项: noneclass

首先,使用 -implicit:none 和不使用 -implicit 选项有什么区别?

其次,关于我的问题标题,我希望所提供的示例能帮助我理解什么是隐式引用文件。非常感谢。

最佳答案

您在 Java documentation 中对其进行了很好的描述.

第一个问题的答案很简单。不使用 -implicit 选项几乎就像使用 -implicit:class (这是该选项的默认值),但通过显式使用该选项可以抑制某些警告:

The compiler might not discover the need for some type information until after annotation processing completes. When the type information is found in a source file and no -implicit option is specified, the compiler gives a warning that the file is being compiled without being subject to annotation processing. To disable the warning, either specify the file on the command line (so that it will be subject to annotation processing) or use the -implicit option to specify whether or not class files should be generated for such source files.

现在你的第二个问题。正如文档所述:

To compile a source file, the compiler often needs information about a type, but the type definition is not in the source files specified on the command line. The compiler needs type information for every class or interface used, extended, or implemented in the source file. This includes classes and interfaces not explicitly mentioned in the source file, but that provide information through inheritance.

For example, when you create a subclass java.applet.Applet, you are also using the ancestor classes of Applet: java.awt.Panel, java.awt.Container, java.awt.Component, and java.lang.Object.

让我们在三个文件中包含三个类:Main、ImplicitClass、BaseImplicitClass。

主要.java:

public class Main {
public static void main(String[] args) {
ImplicitClass ec = new ImplicitClass();
System.out.println(ec.getClass());
}
}

隐式类.java:

public class ImplicitClass extends BaseImplicitClass {
}

BaseImplicitClass.java:

public class BaseImplicitClass {
}

当你像这样从命令行编译它们时:

javac -cp . -implicit:class Main.java

javac -cp . Main.java

所有三个 *.class 文件都已创建。

编译时

javac -cp . -implicit:none Main.java

只创建 Main.class。

编译时

javac -cp . -implicit:none Main.java ImplicitClass.java

创建了两个 *.class 文件,Main.classImplicitClass.class(ImplicitClass 现在变得显式,因为它已经显式传递给编译器),但不是 BaseImplicitClass.class,因为需要此类,但它是隐式的。

关于java - 有人可以提供 javac 的 -implicit 选项如何工作的示例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23520975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com