//只有一个抽象方法的接口,函数式接口
interface Like{
void Method();
}
class Like1 implements Like{
@Override
public void Method() {
System.out.println("普通的类实现");
}
}
//测试
public class Test {
static class Like2 implements Like{
@Override
public void Method() {
System.out.println("静态内部类");
}
}
public static void main(String[] args) {
// 1.普通的创建对象
Like like = new Like1();
like.Method();
// 2.静态内部类
like= new Like2();
like.Method();
// 3.局部内部类
class Like3 implements Like{
@Override
public void Method() {
System.out.println("局部内部类");
}
}
like = new Like3();
like.Method();
// 4.匿名内部类
like = new Like() {
@Override
public void Method() {
System.out.println("匿名内部类");
}
};
like.Method();
// 5.limbda表达式
like = ()->{
System.out.println("Method表达式");
};
like.Method();
}
}
输出结果:
普通的类实现
静态内部类
局部内部类
匿名内部类
Method表达式
Process finished with exit code 0