java9-模块化示例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012326462/article/details/83691729

首先我们在idea中建了一个maven工程,里面有三个子module
module样例工程

  • address module
    address module在address这个module中建了两个包,一个是com.xhx.address,一个是com.xhx.other,其中,我们在module-info.java中把com.xhx.address给公开了。
  • clothes module
    clothes module我们把com.xhx.cloth包给公开了
  • people module
    people module
    首先要在people的pom.xml中引入上两个的依赖,然后在module-info.java中,引入上面两个module。
    App.java
package com.xhx.people;

import com.xhx.address.Address;
import com.xhx.cloth.Clothes;

public class App {
    public static void main(String[] args) {
        Clothes clothes = new Clothes();
        clothes.setId("3f-3faf-fdaa-54qfaf");
        clothes.setColor("red");
        clothes.setSize("3xl");
        System.out.println(clothes.toString());

        Address address = new Address();
        address.setContry("CN");
        address.setProvince("hebei");
        System.out.println(address.toString());

        //因为com.xhx.other没有 exports,所以Car类引不到
       // new Car()
    }
}

此时,如果我们引入没有被公开的包com.xhx.other,会提示错误
在这里插入图片描述
程序运行结果如下:
运行结果
这个简单的程序,大家应该对模块化有所理解了。后面博客再继续深入讲解。

github代码地址

猜你喜欢

转载自blog.csdn.net/u012326462/article/details/83691729