/*
匿名对象:没有名字的对象;
*/
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());
}
}
|