匿名内部类

package oop.demo12;

public class Test {
    public static void main(String[] args) {
        //没有名字的初始化,叫做匿名内部类
        new Apple().eat();//不用将实例保存到对象中

        new UserService(){
            @Override
            public void hello(){

            }
        };
    }
}

class Apple{
    public void eat(){
        System.out.println("1");
    }
}

interface UserService{
    void hello();
}

 

posted @ 2022-02-12 16:53  狂神大鑫  阅读(20)  评论(0)    收藏  举报