What is a software package in Java?
Why use a package?
How to package a class?
//To place a class in a package, you need to use the package name//the method of packaging compilation javac-d. Test.java//Error: Encoding GBK non-mapped character javac-encoding UTF-8-D. Test.java//-D generates a folder based on the package name Safiri. Generate the Safiri folder in the current directory//The full name of a class should be "package name" + "." + "class name" Safiri. Test//run: Java Safiri. Test//naming conventions for package names://1. Request for registration All letters are lowercase//2. Package name in general, is your domain name upside down write Com.sdau the full name of the class is Com.sdau.Test PackageSafiri;classtest{ Public Static voidMain (String args[]) {System.out.print ("Hello Package"); }}//1. Package provides a namespace for Java classes//2. Packaging requires the use of the package Directive//3. The full name of a class should be "package name." + "class name"
Access Permissions in Java:
public: Common permissions for cross-package access to use classes and members of classes when you are using the member (no restrictions, classes in the same package or in different packages are free to access)
Private: the member variables and functions that are modified by the proprietary permissions can only be used in this class, and other classes cannot be used (only in this class)
Default: Package-level access to classes within the same package can access the default permission's class and its members (the Write permission modifier is the default permission, which is freely accessible in the same package)
protected: The protected permission first has the same functionality as default, but the permission can only decorate member variables and member functions (the other 3 can also decorate the class) to allow subclasses to use protected to decorate the members of the parent class with the members of the parent class in the different packages.
Public >protected >default >private
Import of packages
It is cumbersome to write a package name + class name when using classes across packages, so there is an import mechanism when the package name is relatively long:
Import Org.marsdroid.Person;
After importing and then using this class, you do not have to write the package name, write the class name directly.
Access rights and inheritance
If the child class and the parent class are not in the same package, the subclass can inherit the member variables and member functions of the default permission in the parent class, but cannot be used because of insufficient permissions.
Inheriting members of the stepfather class can be used, but also consider the permissions of members