//格式
public class 类名 implements 接口名{
}
//示例
public class test{
public static void main(String[] args){
A a=new A();
a.eat();
}
}
//定义接口
interface Inter{
void eat();
}
//定义实现接口类
class A implements Inter{
public void eat(){
System.out.println("吃饭");
}
}
二.匿名内部类实现接口
//格式
new 接口名(){
public 返回值类型 重写的方法名(){
}
};
//示例
public class Test1 {
public static void main(String[] args) {
//使用匿名内部类实现接口
Inter inter=new Inter() {
public void eat() {
System.out.println("吃饭");
}
};
inter.eat();
}
}
interface Inter{
void eat();
}
三.Lambda表达式实现接口
public class Test1 {
public static void main(String[] args) {
go(new Inter() {
public void eat() {
System.out.println("去吃饭");
}
});
go(() -> System.out.println("去吃饭"));
}
public static void go(Inter inter){
inter.eat();
}
}
interface Inter{
void eat();
}