黑马程序员技术交流社区

标题: 匿名对象 [打印本页]

作者: 374856298    时间: 2015-5-5 23:02
标题: 匿名对象
/*
        匿名对象:
                是没有名字的对象。

        应用场景:
                A:当对对象方法仅进行一次调用的时
                B:匿名对象可以作为实际参数进行传递
*/

class Student
{
        public void show()
        {
                System.out.println("student -- show");
        }
}

class Test
{
        //引用类型作为形式参数
        public void print(Student s)
        {
                s.show();
        }

        public void print(int a)
        {
                System.out.println(a);
        }
}

class NiMingTest
{
        public static void main(String[] args)
        {
                //如何使用show()方法呢?
                Student s = new Student();
                s.show();
                s.show();
                s.show();

                //匿名对象的使用
                new Student().show();
                new Student().show();
                new Student().show();


                Test t = new Test();
               
                //Student s2 = new Student();
                //t.print(s2);

                t.print(new Student());
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2