gpt4 book ai didi

java - 为什么我们需要在 java-9 模块系统中 requires static?

转载 作者:搜寻专家 更新时间:2023-11-01 02:19:00 26 4
gpt4 key购买 nike

<分区>

我开始学习拼图 java-9 功能并阅读了一些文章/视频。

我无法理解可选依赖项的概念(需要静态)

引自 article :

When a module needs to be compiled against types from another module but does not want to depend on it at run time, it can use a requires static clause. If foo requires static bar, the module system behaves different at compile and run time:

At compile time, bar must be present or there will be an error. During compilation bar is readable by foo.
At run time, bar might be absent and that will cause neither error nor warning. If it is present, it is readable by foo.

所以我想知道几件事:

  1. 为什么要让模块在编译时依赖于另一个模块而不是在运行时?有什么例子吗?像 lombok 这样的乐器?

  2. java-9 之前的 java 中可选依赖项的任何类似物?

附言

我又找到了一个解释:引自 article :

Sometimes we write code that references another module, but that users of our library will never want to use.

For instance, we might write a utility function that pretty-prints our internal state when another logging module is present. But, not every consumer of our library will want this functionality, and they don’t want to include an extra logging library.

In these cases, we want to use an optional dependency. By using the requires static directive, we create a compile-time-only dependency:

module my.module {
requires static module.name;
}

但我完全不清楚。谁能用简单的方式解释一下?

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