【java基础】接口是否能有实现类?

接口是否能有实现方法
个人回答: 固然能够

java8之后就容许接口有实现方法:java

  • default修饰的方法
  • static修饰的方法
/**
 * 能用lambda的状况,接口里面只有一个未实现的方法
 * 保证函数式接口@FunctionalInterface,若是有两个方法就会报错
 */
public class LambdaDemo {

    @FunctionalInterface
    interface Age {
        int add(int x, int y);
        //能够随便有几个default
        default int add2(int x, int y){
            return x + y + 2;
        };
        //static方法也能够有方法体,能够随便写几个
        public static int add3(int i, int y) {
            return i + y;
        }
    }

    public static void main(String[] args) {
        Age age = (int x , int y)-> {
            return x + y;
        };

        System.out.println("" + age.add(2,3));
        System.out.println("Age" + Age.add3(3,4));
    }
}

能够关注个人公众号一块儿学习
函数

相关文章
相关标签/搜索