packageJava Advanced 06_02_ Object-oriented;* A: Call method: this method is called only once * Note: it is not appropriate to call multiple times. * So what are the benefits of this anonymous object? * Yes, anonymous objects are garbage after being called and can be collected by the garbage collector. * B: Anonymous objects can be passed as arguments */
public class NoNameDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
/ / the whole staff
Student student =new Student();
student.show();// Call the Student show method and output
/ / temporary workers
new Student().show();// Call the Student show method and output
new StudentDemo().method(new Student());
StudentDemo (method); StudentDemo (method); StudentDemo (Student); StudentDemo (method); StudentDemo (Student)
Student stu = new Student();
StudentDemo studentDemo = new StudentDemo();
studentDemo.method(stu);StudentDemo (method); StudentDemo (method); StudentDemo (Student); StudentDemo (method); StudentDemo (Student)}}class Student{
void show(a) {
System.out.println("I am Zhang Guowei, if you refuse to obey, do it."); }}class StudentDemo{
// The method in this class calls the show method in Student.
void method(Student stu) { stu.show(); }}Copy the code