The concept of "java basics of" Java packages

First, what is java package

Java programs, in order to facilitate the development, similar to the function of the class will be placed in a folder, the file structure is the package.

Two, java naming packages in

Anti write domain name, for example: com.baidu.

Third, the use of java package

Package Penalty for demo.test; 

public  class Dog { 
    String name; 
    int Age;
     public  void Bark () { // bark 
        System.out.println ( "bark, do not come" ); 
    } 
    public  void Hungry () { // hunger 
        System .out.println ( "master, I'm hungry" ); 
    } 
}

Where: package demo.test; is defined in the package: the need for and the actual name of the directory structure consistent.

Fourth, access to the package in java

package demo.basics;

import demo.test.Dog;

public class Demo {
    public static void main(String[] args){
        Dog dog = new Dog();
        dog.bark();
    }
}

operation result:

Access classes in different packages need to add: import demo.test.Dog;

Two ways of introducing the class

One way: you use such as Bus class, the class you want to import by import keyword

How to write, that is the full path of this class, is also (.) Split

For example: Import demo.test.Dog;  // Note that this is also the last semicolon

Second way:

For example, you use all the classes of a package, you can write

import demo.test.*;

It demo.test is a package name, the package store all the fruits, if I have to use, then I have to come in one by one to import the class (no auto-import so powerful editing tools before, when work is quite boring!)

If it does not come in a need to use a wildcard import, this is the wildcard asterisk (*).

Such a lot of trouble is omitted, while also reducing the amount of code.

Reference: https://baijiahao.baidu.com/s?id=1610064438454407570&wfr=spider&for=pc

 

Guess you like

Origin www.cnblogs.com/jssj/p/11260840.html