First, the concept of package: Create, use.
1. New Package:
In the top row, no more code can be found before.
Package name;
Note: The package name generally has a named specification. For example: Com.itnba.maya.zy (from large to small).
2. Use the package:
Import package name. *;
Import package name. class name;
For example: Import com.itnba.maya.zy.*;
Note: Import should not be placed on the top of the package
Package Com.itnba.maya.zy; import com.itnba.maya.zyy.*; // To quote from another package. Public class Zz { publicstaticvoid main (string[] args) { new // to be referenced }}
Second, constant. Final type variable
There are no real constants in Java. Only the final type of variable can be used as represented.
The final variable, only two places can be assigned, the rest of the place is not on the left.
1. You can assign values at the time of definition.
2. You can assign a value in the constructor. If you assign a value at definition, you cannot assign a value in the constructor. Includes a null value when defined, it is no longer assignable in the constructor.
Public class Test1226 { final// define a final variable (constant)public Test1226 () { // Assign Value } }
Third, static members
Keywords: static, corresponding to "static" is called "instance"
Static variables and static methods. --instance variables and instance methods.
Instance variables and instance methods belong to each individual object. Static variables and static methods are shared by all objects, which belong to the class.
Static variables when storing data must be careful to prevent sharing conflicts.
Static methods can only manipulate static variables and static methods of the current class, and cannot manipulate instance variables and instance methods. The static and static methods of the current class can be called in an instance method.
Single-class mode. Singleton mode. Only one object can be generated. ---case. Private constructor. static methods.
The first step: Set the constructor to private.
Step two: Make a static member variable of the current type.
Step three: Do a static member method of public. Used to instantiate the member variable in the second step and return back.
Public classMao {PrivateString Mingzi; Private StaticMao AAA; PrivateMao () {//the constructor is private.Mingzi = "Cat"; } Public StaticMao Zao () {//with static adjustment if(AAA = =NULL) {AAA=NewMao ();//only new once judged } returnAAA; } PublicString Getmingzi () {returnMingzi; }}
Public class Home { publicstaticvoid main (string[] args) { // Mao A = new Mao (); The constructor can't be adjusted. Mao M1 = Mao.zao (); System.out.println (M1.getmingzi ()); = Mao.zao (); System.out.println (M2.getmingzi ()); // M1 and M2 are the same. }}
Iv. inheritance
Key words: Extends
You can inherit related content from the parent class directly into the subclass, and the subclass does not need to be redefined.
Private, the member cannot be inherited
Public,protected is always inherited.
By default, only the current package can be inherited.
The member variable of a subclass has two operations with a member method: one is an extension and the other is an override.
Public class Dongwu { protected String name= "Animal"; Public void Jiao () { System.out.println (name); } }
public class niao extends Dongwu { public Span style= "COLOR: #0000ff" >void Jiao () {//inherit this . Name); // inherit name }}
Public class Home { publicstaticvoid main (string[] args) { new Niao (); A.jiao (); }} // instantiation of
Concepts, constants, static and inheritance of packages in Java