Boring JavaEE from entry to abandonment (6) package mechanism & static import

1. Package mechanism

The package mechanism is an important means of managing classes in Java. During development, we will encounter a large number of classes with the same name. Through packages, we can easily solve the problem of class duplication and realize effective management of classes.
The package is for the class, and the folder is for the file.

We implement the management of the class through the package. There are two main points in the use of the package:
1. It is usually the first non-commentary sentence of the class.
2. Package name: Write the domain name backwards, plus the module name, which is convenient for internal management.

 

It is recommended to import the details when importing, for example:

import com.han.test.Student;
import com.han.test.User;

If it is import com.han.test.*;, the compilation speed will be affected, but the running speed will not be affected.

When there are classes with the same name in the two imported packages, then write all the paths when quoting in the code, eg: com.han.test.Student stu=new com.han.test.Student();

The classes under the java.lang package do not need to be imported and can be used directly, such as String, Math, Integer, System and Thread.

2. Static import

After the static import, you can use the class name directly!

eg:

Just like the following statically imported functions of the Math library, you can use the function directly, instead of having to use Math. like the commented sentence.

package com.company;
import com.company.Test.Student;
import static java.lang.Math.*;

public class Main {

    public static void main(String[] args) {
        System.out.println("尚尚,你真好!");
        Student stu=new Student();
        //System.out.println(Math.sqrt(9));
        System.out.println(sqrt(9));
    }
}

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/115322774