求解,用内部类,写一下代码和注释,

有一个铃声接口类Bell,里面一个接口方法sound,
有一个手机类Cellphone,具有闹钟功能alarmclock方法,有一个参数是Bell类型,
测试手机类的闹钟功能alarmclock方法,通过匿名内部类作为参数传值,
通过Bell的sound方法打印:懒猪起床了。


interface Bell{
    void sound();

}
class CellPhone {
    public void alarmClock(Bell bell){
        System.out.println("懒猪起床了");
    }
}
public class Test {
    public static void main(String[] args) {
        CellPhone cellPhone= new CellPhone();
        cellPhone.alarmClock(
                new Bell() {
                    @Override
                    public void sound() {
                    }
                }
        );
    }
}

这样啦

interface Bell{
void sound();

}
class CellPhone {
public void alarmClock(Bell bell){
}
}
public class Test {
public static void main(String[] args) {
CellPhone cellPhone= new CellPhone();
cellPhone.alarmClock(() -> System.out.println("懒猪起床了"));
}
}
很高兴得到你的采纳!!!

@FunctionalInterface
interface Bell{
    void sound();

}
class CellPhone {
    public void alarmClock(Bell bell){
        bell.sound();
    }
}
 public class Test {
    public static void main(String[] args) {
        CellPhone cellPhone = new CellPhone();
        cellPhone.alarmClock(() -> System.out.println("懒猪起床了"));
    }
}

对比下,上面2个都有错误