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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zlp19940327 中级黑马   /  2015-4-15 00:37  /  231 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
        匿名对象:没有名字的对象;
       
*/
class Student
{
        String stuName;
        int age;

        void show(){
                System.out.println("Student --> show()");
        }
}
class F
{
        void print(Student stu){
                System.out.println(stu.stuName);
                System.out.println(stu.age);
        }
}
class Demo
{
        public static void main(String[] args)
        {
                Student s1 = new Student();
                F fObj = new F();
                fObj.print(s1);
                //如果s1在之后使用不到,只在这个方法调用时使用一次,这时可以使用匿名的Student对象
                fObj.print(new Student());
                //使用匿名的F对象
                new F().print(new Student());
        }
}


评分

参与人数 1黑马币 +8 收起 理由
zeng1994 + 8 很给力!

查看全部评分

0 个回复

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