匿名内部类练习题

public class Ttest {
    public static void main(String[] args) {
        Cellphone c1 = new Cellphone();
        c1.naozhong(new Bell() {
            @Override
            public void ring() {
                System.out.println("懒猪起床了");
            }
        });
        System.out.println("++++++++++++++");
        Cellphone c2 = new Cellphone();
        c2.naozhong(new Bell() {
            @Override
            public void ring() {
                System.out.println("起床上课了");
            }
        });
        c2.naozhong(() -> {
            System.out.println("Lambda表达式-->简化匿名内部类的代码写法");
        });//Lambda表达式使用前提  必须是接口的匿名内部类,接口中只能有一个抽象方法
    }
}

interface Bell {
    void ring();
}

class Cellphone {
    public void naozhong(Bell bell) {
        bell.ring();
    }
}
posted @ 2022-11-14 20:59  微风抚秀发  阅读(50)  评论(0)    收藏  举报