package Java高级06_02_面向对象;
/*
* 匿名对象:就是指没有名字的对象称之为匿名对象
* 匿名对象的应用场景:
* A:调用方法:这个方法仅仅调用一次
* 注意:调用多次的时候不合适。
* 那么这种匿名对象有什么好处?
* 有,匿名对象调用完毕之后就是垃圾,可以被垃圾回收器进行回收。
* B:匿名对象可以作为实参进行传递
*/
public class NoNameDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//全职工
Student student =new Student();
student.show();//调用Student类中的show方法输出:在下张国伟,不服来干
//临时工
new Student().show();//调用Student类中的show方法输出:在下张国伟,不服来干
new StudentDemo().method(new Student());
//调用StudentDemo类中的method方法,method方法调用Student类中show方法输出:在下张国伟,不服来干
Student stu = new Student();
StudentDemo studentDemo = new StudentDemo();
studentDemo.method(stu);//调用StudentDemo类中的method方法,method方法调用Student类中show方法输出:在下张国伟,不服来干
}
}
class Student{
void show() {
System.out.println("在下张国伟,不服来干");
}
}
class StudentDemo{
//这个类中的方法之调用Student中的show方法而已。
void method(Student stu) {
stu.show();
}
}