1 package com.fu.java1; 2 3 /** 4 * 抽象类的匿名子类 5 * 6 */ 7 public class PersonTest { 8 public static void main(String[] args) { 9 method(new Student());//匿名对象 10 Worker worker = new Worker(); 11 method1(worker);//非匿名的类,非匿名的对象 12 13 method1(new Worker());//非匿名的类,匿名的对象 14 System.out.println("*************"); 15 16 //创建了一匿名子类的对象:p 17 Person p = new Person(){ 18 @Override 19 public void eat() { 20 System.out.println("吃"); 21 } 22 23 @Override 24 public void breath() { 25 System.out.println("呼吸"); 26 } 27 }; 28 method1(p); 29 System.out.println("*******************"); 30 //创建匿名子类的匿名对象 31 method1(new Person() { 32 @Override 33 public void eat() { 34 System.out.println("吃个屁"); 35 } 36 37 @Override 38 public void breath() { 39 System.out.println("窒息"); 40 } 41 } 42 ); 43 } 44 public static void method1(Person person){ 45 person.eat(); 46 person.breath(); 47 // person.walk(); 48 } 49 public static void method(Student student){ 50 51 } 52 } 53 54 class Worker extends Person{ 55 public void eat(){ 56 // System.out.println("学生应该吃有营养的食物"); 57 } 58 59 @Override 60 public void breath() { 61 // System.out.println("学生应该多呼吸新鲜的空气"); 62 } 63 }