需求和需要Java 9中的传递语句有什么区别? [英] What's the difference between requires and requires transitive statements in Java 9?

查看:217
本文介绍了需求和需要Java 9中的传递语句有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模块声明中 需要 需要传递 模块语句之间有什么区别?


例如:

  module foo {
需要java.base;
需要传递java.compiler;
}


解决方案

可读性回顾



如果模块 bar 需要模块饮料,那么模块系统。 ..




  • 强制存在饮料(称为可靠配置

  • 允许 bar 读取饮料(称为



    出于这个原因,引入了隐含的可读性:使模块在其自己的公共API中使用另一个模块的类型立即可用而不需要要求调用者追捕并要求所有涉及的模块。



    因此,如果栏需要传递饮料客户可以开始购买饮料而无需需要饮料 - 要求栏就足够了。应该如此。


    What's the difference between requires and requires transitive module statements in module declaration?

    For example:

    module foo {
        requires java.base;
        requires transitive java.compiler;
    }
    

    解决方案

    Readability recap

    If module bar requires module drink, then the module system...

    • enforces the presence of drink (called reliable configuration)
    • allows bar to read drink (called readability)
    • allows code in bar to access public classes in exported packages in drink (called accessibility)

    Exactly the same happens if bar requires transitive drink - drink must be present, can be read and accessed. In fact, for bar and drink the transitive keyword doesn't change anything.

    Implied readability

    The modules depending on bar are the ones that are impacted by transitive: Any module that reads bar can also read drink. In other words readability of drink is implied (which is why this is called implied readability). A consequence is that customer can access drink's types.

    So if bar requires transitive drink and customer requires bar, then customer can read drink even though it doesn't explicitly depend on it.

    Use cases

    But why? Imagine you have a module whose public API accepts or returns another module's type. Let's say the bar module publicly returns instances of Drink, an interface from the drink module:

    // in module _bar_
    public class Bar {
    
        // `Drink` comes from the module _drink_,
        // which _bar_ requires
        public Drink buyDrink() { /* ... */ }
    
    }
    

    In this example, bar uses a regular requires for drink. Now say, customer depends on bar, so all its code can call Bar::buyDrink. But what happens when it does?

    The module system complains that customer does not read drink and can hence not access Drink. To fix that, customer would also have to depend on drink. What a chore! How useless is a bar that you can't use straight away?

    For this reason, implied readability was introduced: to make a module that uses another module's types in its own public API instantly usable without requiring the caller to hunt down and require all involved modules.

    So if bar requires transitive drink, customer can start buying drinks without having to require drink - require bar suffices. As it should.

    这篇关于需求和需要Java 9中的传递语句有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆