A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 374856298 中级黑马   /  2015-5-5 23:02  /  144 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
        匿名对象:
                是没有名字的对象。

        应用场景:
                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());
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马