首页 > 其他分享 >匿名内部类练习题

匿名内部类练习题

时间:2022-11-14 21:12:12浏览次数:41  
标签:练习题 Cellphone 内部 void System 匿名 ring public out

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();
    }
}

标签:练习题,Cellphone,内部,void,System,匿名,ring,public,out
From: https://www.cnblogs.com/weifengfuxiufa/p/16890404.html

相关文章