(day9)Anonymous Inner Class(匿名内部类)

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(){

    }
})
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容