Java keywords (1): package and import



foreword

This blogger will use CSDN to record the experience and knowledge gained and learned on the road of software development and learning. Interested friends can follow the blogger!
Maybe a person walking alone can go very fast, but a group of people walking together can go further! Let us learn from each other on the road of growth, welcome to pay attention!

Previous: Process-Oriented and Object-Oriented

1. Use of the "package" keyword

1 Overview

(1) In order to better realize the management of classes in the project, the concept of package is provided.

(2) packageStatement As the first statement of the Java source file, it indicates the package where the class defined in the file is located. (If this statement is defaulted, it is specified as an unnamed package).
Its format is:package 顶层包名.子包名 ;

package pack1.pack2; //指定类PackageTest属于包pack1.pack2
      public class PackageTest{
     
     
        public void display(){
     
     
          System.out.println("in method display()");
        }
}

(3) Packages, belonging to identifiers, follow the naming rules of identifiers, specifications (xxxyyyzzz), "see the name and know the meaning", usually identified with lowercase words.

(4) The package corresponds to the directory of the file system. In the packagestatement, "." is used to indicate the level of the package (directory), and each "." represents a layer of file directory.

(5) Under a package, interfaces and classes with the same name cannot be named. Under different packages, interfaces and classes with the same name can be named .

2. Function

(1) Packages help manage large software systems: Classes with similar functions are divided into the same package. For example: MVCthe design pattern.

MVC design pattern :

MVC is one of the commonly used design patterns, which divides the entire program into three layers: the view model layer, the controller layer, and the
data model layer. This design pattern, which separates program input and output, data processing, and data display,
makes the program structure flexible and clear, and also describes the communication method between program objects, reducing program coupling.

  • The model layer model mainly processes data

    • Data object encapsulates model.bean/domain
    • Database operation class model.dao
    • database model.db
  • The control layer controller handles business logic

    • Application interface related controller.activity
    • store fragment controller.fragment
    • Display list of adapters controller.adapter
    • Service related controller.service
    • Extracted base class controller.base
  • view layer view display data

    • Related tools class view.utils
    • custom view view.ui
      insert image description here

(2) Packages can contain classes and sub-packages to divide the project hierarchy and facilitate management.

(3) Solve the problem of class naming conflicts.

(4) Control access rights.

3. Introduction to the main packages in JDK

Package names Features
java.lang Contains some Java language core classes, such as String, Math, Integer, Systemand Thread, providing common functions
java.net Contains classes and interfaces that perform network-related operations
java.io Contains classes that provide various input/output functions
java.util Contains some utility classes, such as collection framework classes that define system properties, interfaces, and use functions related to date and calendar
java.text Contains some java formatting related classes
java.sql Contains java related classes/interfaces for JDBC database programming
java.awt Contains classes that make up the abstract window toolkits, which are used to build and manage the application's graphical user interface (GUI)


2. Use of "import" keyword

1 Overview

(1) In order to use Java classes defined in different packages, a importstatement is required to introduce the class or all classes required under the specified package hierarchy, and the importstatement tells the compiler where to look for the class.
(2) Syntax format:import 包名. 类名

import pack1.pack2.Test; //import pack1.pack2.*;表示引入pack1.pack2包中的所有结构
public class PackTest{
     
     
   public static void main(String args[]){
     
     
      Test t = new Test(); //Test类在pack1.pack2包中定义
      t.display();
    } 
}

(3) Explicitly use the importstructure in the source file to import the classes and interfaces under the specified package.

(4) importStatements are declared between the package declaration and the class declaration.

(5) If you need to import multiple structures, you can write them in parallel.

(6) The “ xxx.*” method can be used, indicating that all structures under the xxx package can be imported, such as: using java.util.*the method, utilall classes or interfaces under the package can be imported at one time.

(7) If the imported class or interface is java.langunder the package, or under the current package, this importstatement can be omitted.

(8) If a class with the same name under different packages is used in the source file, at least one class must be displayed in the form of the full class name.

java.a(9) If the class under the package has been imported . Then if you need to use the classes under the subpackages of the a package, you still need to import them.

(10) import staticCombination use: call static properties under the specified class or interface or

2. Code demo

package com.atguigu.java2;

import java.lang.reflect.Field;
import java.util.*;
import com.test1.exer4.Account;
import com.test1.exer4.Bank;
import com.test1.java2.java3.Dog;
import static java.lang.System.*;
import static java.lang.Math.*;

public class PackageImportTest {
     
     
	public static void main(String[] args) {
     
     		
		String info = Arrays.toString(new int[]{
     
     1,2,3});		
		Bank bank = new Bank();		
		ArrayList list = new ArrayList();
		HashMap map = new HashMap();		
		Scanner s = null;
		
		System.out.println("hello!");
		
		Person p = new Person();		
		Account acct = new Account(1000);
		//全类名的方式显示
		com.test1.exer3.Account acct1 = new com.test1.exer3.Account(1000,2000,0.0123);		
		Date date = new Date();
		java.sql.Date date1 = new java.sql.Date(5243523532535L);		
		Dog dog = new Dog();		
		Field field = null;		
		out.println("hello");		
		long num = round(123.434);
	}
}

Guess you like

Origin blog.csdn.net/weixin_52533007/article/details/123971595