Goal
1, Block
2. GC (Learn)
3. Package Import
4. Encapsulation
One, block
{} classification
1, ordinary block
function: Organize code, solve the scope of variables, save memory.
A variable with the same name cannot be declared within the same scope.
2. Static block: statics {}
When you use a class, it is loaded only once
The result of the execution is:
3. Building block: {}
Executes before the constructor, once for each object created.
Second, GC
Garbage collection garbage collection mechanism
1. The programmer is not authorized to call the garbage collector.
2, the programmer can pass System.GC (). Notifies the GC to run, but the Java specification does not
Guaranteed to run immediately.
3. Finalize method is the method that Java provides to programmers to release objects or resources.
But try to use less.
Third, package
When working, each class must have a package.
1. function
Organize and manage, avoid duplicate names
2. Naming
Company domain name inverted com.bjsxt. function | module –> Note all lowercase
Note Position: First line, first row, before class
Attention:
1), not operating system keywords and Java keywords
Con,prn,aux,clock$,nul,com1,com2,com3,com4,com5,com6,com7,com8,com9,lpt1
2), do not define a class with the same name as the JDK
3. Full path (fully qualified name)
Package name. class Name ÀCOM.BJSXT.OOP.TESTPK
Java.util.Scanner
4. JDK Common Package
Java.langà Language Pack
Java.utilà Tool Kit
Java.ioàio Flow
Java.sqlà Operating Database
5. Import imported Class
Easy to use class, omit package name, do not write full path every time
1), Function:
After import: Omit full path
2), do not import the package
The following packages do not have to be imported (auto Import, nearest principle)
1), same package
2), Java.lang
3), *
If you are using more than one class under a package,
You can use a qualifier *
4), cannot import a class with the same name at the same time
When encountering multiple packages under the same name of the class, one import, the other writes the full path.
5), Import static
Import a static property under a class, static method, and must have access permission
Iv. Package Encapsulation1, purpose
Cohesion Poly-low coupling achieves hidden interior detail
2. Visibility (access rights)
A), public Wang Jianlin personal information
b), protected protected heritage son use à in subclasses and subclasses use
c), default BMW son family
b) Private, privately-owned Wang Jianlin love letters
Access rights are reduced in turn:
This class private default protected public
Same package class (including subclass) default protected public
Subclasses of different packages protected public
Other classes of different packages public
1), Class: Public default
2), Method: Privatedefault protected public
3), Properties: Privatedefault protected public
3. JavaBean (PO BO VO)
1), Role: Encapsulation of private data, external provide a large number of public setter (storage) and getter (get) accessors, a small number of other methods.
2), Property privatization
3), accessor: setxxxgetxxx
Naming conventions
A case where the first letter is not allowed to capitalize the second lowercase letter.
Examples are as follows:
4. Single Case design mode
Goal: A class has only one object to the outside.
|
Package com.bjsxt.encapsulation;
/**
* A hungry man type single case design mode
* 1, constructor privatization--Avoid external new call constructor
* 2. Provide public static methods for external access to an object
* 3, write a static property, instantiate the object
* @author Administrator
*
*/
Publicclass Eagersingleton {
Provides a static property that instantiates the object
privatestatic Eagersingletones =newEagersingleton ();
Constructor privatization
Private Eagersingleton () {
}
Provides public static methods to obtain the object
publicstatic Eagersingletongetinstance () {
return es;
}
}
|
|
Package com.bjsxt.encapsulation;
/**
* Lazy Single-case design mode
* 1, constructor privatization--Avoid external new call constructor
* 2, external public static method, the first time you get the object created
* 3, write a static property, do not instantiate the object
* @author Administrator
*
*/
Publicclass Lazysingleton {
Provides a static property and does not instantiate the object
privatestatic Lazysingletones ;
Constructor privatization
Private Lazysingleton () {
}
Provides public static methods to obtain the object
publicstatic Lazysingletongetinstance () {
if (es = =null) {//If first time access
es =newLazysingleton ();
}
return es;
}
}
|
Sixth day of Java learning