One, the package
Use the PACKAGE keyword to declare packages. The role of a package is equivalent to a namespace. If the package to which the class belongs is not explicitly declared, then the class is placed in the default package, and the default package does not have a name. Package support is created hierarchically, which supports nesting.
Import the package using the Import keyword. Supports importing a class or package from a package, and if you do not import a declaration variable, you need to write the full path.
import java.lang.*; // Importing Packages import java.util.ArrayList; // Import Class class Solution { ArrayList ArrayList; // has been imported Java.util.LinkedList LinkedList; // not imported }
Second, the interface
Declares an interface. The normal method of an interface cannot exist in a specific implementation.
Interface Color { void setcolor (int color); int GetColor ();} Interface Line { void setLength (int length); int getlength ();}
Implements the interface. Class can implement multiple interfaces at the same time.
classColorfullineImplementsColor, line {intcolor; intlength; Colorfulline (intColorintlength) { This. color =color; This. length =length; } @Override Public voidSetColor (intcolor) { This. color =color; } @Override Public intGetColor () {returncolor; } @Override Public voidSetLength (intlength) { This. length =length; } @Override Public intGetLength () {returnlength; }}
nested interfaces. When an interface is a member of a class or other interface, there are more access restrictions to choose from.
class A { publicinterface I { boolean isnegative (int x) ; }} class Implements A.I { @Override publicboolean isnegative (int x) { return x < 0; }}
interface variables. You can use interfaces to import shared constants into multiple classes.
Interface RBG { int RED = 0; int BLUE = 1; int GREEN = 2;}
Extension interface. Interfaces can inherit other interfaces.
Interface extends RBG { int ORANGE = 3; int YELLOW = 4; int CYAN = 5; int PURPLE = 6;}
The default implementation. Interfaces can declare default methods and can be overridden.
Interface I { defaultvoid Show () { System.out.println ("Hi");} } class Implements I { @Override publicvoid Show () { System.out.println (" Hello ");} }
Variable members and static methods can be called directly.
Interface I { int val = 0; Static void Show () { System.out.println (val); }} class Solution { publicstaticvoid main (string[] args) { I.show (); System.out.println (I.val); }}
Java notes: Packages and Interfaces