匿名内部类,接口类型的方法调用,使用匿名内部类day10

/*
    接口类型的方法调用,使用匿名内部类
    匿名内部类:
        语法定义格式:
            new 抽象类/接口(){
               //要重写的方法
            }
 */

interface Inter1{
    void fun1();
}

//class Inter1Impl implements Inter1{
//    @Override
//    public void fun1() {
//        System.out.println("hello java");
//    }
//}


class Student2{
    public void show(Inter1 inter1){
        inter1.fun1();
    }
}

public class NiMingClassDemo2 {
    public static void main(String[] args) {
        Student2 student2 = new Student2();
//        student2.show(new Inter1Impl());
        student2.show(new Inter1(){
            @Override
            public void fun1(){
                System.out.println("hello 数加");
            }
        });

        student2.show(new Inter1(){
            @Override
            public void fun1(){
                System.out.println("hello java");
            }
        });

//        student2.show(()-> System.out.println("hello world")); // lambda表达式
    }
}
posted @ 2024-08-06 21:27  ていせい  阅读(2)  评论(0编辑  收藏  举报