匿名对象:没有名字的对象。
场景:
1:当对对象方法仅进行一次调用的时
2:匿名对象可以作为实际参数进行传递
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();
t.print(new Student());
}
}
OK,谢了!!多敲敲代码,让自己也记得牢一点,(*^__^*) 嘻嘻…… |
|