Python Engineer's Road to Java (p) Module and Package

Module is usually translated as module , and Package is usually translated as package.

1. Python Module and Package

  • Python module (Module): 1 .pyfile ending
    with variables, functions, and classes that can be defined in the module for external use,
    such as: from 包.模块 import 函数, from 包 import 模块,import 模块
  • Python package (Package): essentially a directory (Directory).
    When the package is imported, the contents in the package __init__.pywill be executed.



After creating the Python Package , there is still a__init__.py

2. Java Module and Package

  • In IDEA, Project is the highest storage directory. When creating a Project, it is accompanied by the creation srcand pom.xml
    creation. After creating the Project, you do not need to create a Model, or you can create multiple Models under the Project.
  • After the Module is created, it will come srcwith it. pom.xml
    Multiple Packages can be created under the Module.
  • Multiple Java classes can be created under Package

The relationship between Project, Module and Package in IDEA

2.1、Module

Use IDEA to create a Module under Project

After creating the Module, there will be an inheritance relationship pom.xmlbetween the Module and the Project in the Module.

<parent>
    <groupId>org.example</groupId>
    <artifactId>project01</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

At the same time, the outermost layer pom.xmlwill add modulesinformation and <packaging>pom</packaging>

<packaging>pom</packaging>
<modules>
    <module>module01</module>
    <module>module02</module>
</modules>

You can also create a Module under Module, and the child Module will inherit the parent Module.

2.1.1. The significance of sub-module development

Enhance the scalability of the project to facilitate other projects to reference the same functions.

Each module can be independently maintained by different teams.

Split the original module into several sub-modules according to functions to facilitate mutual calls and interface sharing between modules.

2.1.2. Module call

module02If you want to call it module01, you need to module02add pom.xmldependencies (go there module01and paste it)

<dependency>
    <groupId>org.example</groupId>
    <artifactId>module01</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

2.2、Package

Java packages can be multi-layered folders, and the syntax format is:pkg1[.pkg2[.pkg3…]]

JavaPackage

importClasses in Package can be called using

import org.example.Main;

public class Hello {
    
    
    public static void main(String[] args) {
    
    
        Main.main(new String[]{
    
    ""});
    }
}

Guess you like

Origin blog.csdn.net/Yellow_python/article/details/128702867