This chapter mainly reviews some of the relevant knowledge of the classes:
(1) Static:
Static: A resource belonging to a class, accessed using the class name.
Static properties: Only one copy of the variable
Static methods: Are class methods that can be accessed directly using the class name. There is no this reference in a static method
Static code block: a block of code that executes during class loading
Extension: code block, executed during object creation, similar to code in the constructor
Reference Example:
1 /**2 * code block: Executed during object creation, similar to code in the constructor3 * Static code block: Executes during class load. Static code can be used to load static resources only once, such as: Picture loading4 */5 Public classDemo01 {6 Public Static voidMain (string[] args) {7Foo F1 =NewFoo ();8Foo F2 =NewFoo ();9 }Ten } One A classFoo { - //Static code block - Static { theSYSTEM.OUT.PRINTLN ("Load Foo.class"); - } - { -System.out.println ("HI"); +}//code block, rarely used! -}
Run Result: Load Foo.class
Hi
Hi
are in order, please look at the comments above
(2) Anonymous inner class: Anonymous inner class is also an inner class without a name
Because there is no name, the anonymous inner class can only be used once, and it is often used to simplify code writing
But there is also a precondition for using anonymous inner classes: You must inherit from a parent class or implement an interface
Example 1: Implementing an abstract method without using anonymous inner classes
1 Abstract classPerson {2 Public Abstract voideat ();3 } 4 5 classChildextendsPerson {6 Public voideat () {7System.out.println ("Eat Something"); 8 } 9 } Ten One Public classDemo { A Public Static voidMain (string[] args) { -Person p =NewChild (); - p.eat (); the } -}
Operation Result: Eat something
As you can see, we inherit the person class with child, and then we implement an instance of child and transform it upward into a reference to the person class.
However, if the child class here is used only once, wouldn't it be a hassle to write it as a separate class?
The anonymous inner class is introduced at this time.
Example 2: Basic implementation of anonymous inner classes
1 Abstract classPerson {2 Public Abstract voideat ();3 } 4 5 classChildextendsPerson {6 Public voideat () {7System.out.println ("Eat Something"); 8 } 9 } Ten One Public classDemo { A Public Static voidMain (string[] args) { -Person p =NewChild (); - p.eat (); the } -}
Operation Result: Eat something
As you can see, we directly implement the methods in the abstract class person in curly braces.
This allows you to omit the writing of a class
Also, anonymous inner classes can be used on interfaces
Example 3: Using an anonymous inner class on an interface
1 InterfacePerson {2 Public voideat ();3 } 4 5 Public classDemo {6 Public Static voidMain (string[] args) {7Person p =NewPerson () {8 Public voideat () {9System.out.println ("Eat Something"); Ten } One }; A p.eat (); - } -}
Operation Result: Eat something
As can be seen from the above example, as long as a class is (abstract non-abstract) or an interface, the methods in its subclasses can be implemented using anonymous inner classes
The most common situation is in the implementation of multithreading, because to implement multithreading must inherit the thread class or inherit the Runnable interface
An anonymous inner class implementation of the instance 4:thread class
1 Public classDemo {2 Public Static voidMain (string[] args) {3Thread T =NewThread () {4 Public voidrun () {5 for(inti = 1; I <= 5; i++) { 6System.out.print (i + "")); 7 } 8 } 9 }; Ten T.start (); One } A}
Operation Result: 1 2 3) 4 5
An anonymous inner class implementation of the instance 5:runnable interface
1 Public classDemo {2 Public Static voidMain (string[] args) {3Runnable r =NewRunnable () {4 Public voidrun () {5 for(inti = 1; I <= 5; i++) { 6System.out.print (i + "")); 7 } 8 } 9 }; TenThread T =NewThread (r); One T.start (); A } -}
Operation Result: 1 2 3) 4 5
The above example is excerpted from http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html, there is a person lazy to tidy up, forgive me, but the landlord tidy really good
Regain Java Series one Java Foundation (4)