java 匿名对象

来源:互联网 发布:温度监控软件 贴吧 编辑:程序博客网 时间:2024/06/01 21:32

1  匿名对象就是没有名字的对象  如果程序中只是用一次,就可以使用匿名对象的方式

  好处: 匿名对象调用完毕就是垃圾,可以被垃圾回收器回收。

案例:


package csdn.zyl.demo;
public class ClassDemo3 {
 public static void  main(String[] args){
  //非匿名对象
       //  Student student=new Student();
       //  student.tell();
  //匿名对象
  new Student().tell();//student 对象省了
 }
}
class Student{
 public void tell()
 {
  System.out.println("Hello girl");
 }
}

原创粉丝点击