JDK8新特性,给接口添加一个默认实现
在JDK8中,允许给接口本身添加一个默认的实现。用“default”进行修饰。如下实例
package interfacetest; public interface TestInterface { default int add(int x, int y) { return x+y; } int sub(int x, int y); }
package interfacetest; public class TestInterfaceClass implements TestInterface { @Override public int sub(int x, int y) { return x-y; } public static void main(String[] args) { TestInterfaceClass testClass = new TestInterfaceClass(); System.out.println(testClass.add(1, 2)); System.out.println(testClass.sub(1, 2)); } }
运行结果如下