- class Demo_AnonymousInnerClass {
- public static void main(String[] args) {
- PersonDemo pd=new PersonDemo();
- pd.method(new Student()); //Person p=new Student();
- //方法中创建Student对象,调用其方法;
- pd.method(new Person(){
- public void print(){
- System.out.println("Love000");
- }
- });
- //匿名内部类当做参数传递,将其看作一个整体;
- }
- }
- abstract class Person{
- public abstract void print();
- }
- class Student extends Person{
- public void print(){
- System.out.println("Love");
- }
- }
- class PersonDemo{
- public void method(Person p){
- p.print();
- }
- }
复制代码 |
|