首页 > 其他 > 详细

匿名对象

时间:2017-02-16 23:03:21      阅读:196      评论:0      收藏:0      [点我收藏+]


/*
      匿名对象注意事项
      1.一般不会给匿名对象赋予属性值,因为不可获取
      2.两个匿名对象永远都不可能是同一个对象
    匿名对象的应用
    1.如果一个对象需要调用一个方法,而调用完这个方法后该对象不再使用。(简化书写,及早释放内存)
    2.可以作为实参调用一个函数
 */
class Student{
 int num;
 String name;
 public void study(){
  System.out.println("学习");
 }
}

class Teacher
{
 String name;
 
 public void task(){
  System.out.println("布置作业");
 }
}

class Demo18{
 public static void main(String[] args)
 {
  //new Student().name = "二娃";   //匿名对象
  //System.out.println(new Student() == new Student());   // == 用于引用数据类型时,比较的是内存。
  
  //Student s = new Student();
  //s.study();
  
  new Student().study();
  
  
  
  
 }
}

匿名对象

原文:http://www.cnblogs.com/cpp-cpp/p/6407480.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!