The Anonymous Object
We create an anonymous object for this object can only be used once.
class Woker{
private String name;
public String Woker(String name){
this.name = name;
}
public String setName(String name){
this.name = name;
}
public void getName(){
this.name = name;
}
}
public class MyClass{
main(){
//The common way
//This object can be invoked repeatedly
Woker xw = new Woker();
buildHouse(xw);
//The anonymous object
//This object can only be invoked here
buildHouse(new Worker(name:"zhangsan"));
//The anonymouse class
buildHouse(new Woker(name:"MiniWoker"){
// The implement of anonymous class.
//It will display "MiniWoker starts to work"
})
}
public static void buildHouse(Worker a){
System.out.println(a.getName() + " starts to work");
}
}
The Anonymous Class
1.It cannot declare any construtor
2.It must inherits one class or implaments one interface
new Thread(new Runnable(){
@Override
public void run(){
}
})