[Java-based Interface whether to have the implementation class?

接口是否能有实现方法
我的回答: 当然可以

It allows the interface have implementations later java8:

  • default modified method
  • static modification of the method

/**
 * 能用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));
    }
}

No. I can focus on the public to learn together

Guess you like

Origin www.cnblogs.com/amberbar/p/11809649.html