Default Methods实例解析
Oracle官网是这样介绍默认方法的,使用默认方法,可以达到往接口里面增加新的功能,而且保持与老版本代码兼容,也就是原来的实现类可以不需要被动修改。所以,默认方法位置是在接口里面;默认方法具有实现,不会强制具体类来现。Java8要充分利用Lambda,需要增强大量的类库,但是又希望做到兼容性,只能用默认方法这个大招。
默认方法
默认方法与普通的接口方法相比,最前面增加default关键字,参数列表之后接大括号,实现该方法,再后面没有分号。
如果翻看jdk源码的interface,会发现多了default关键词。
简单介绍下。
default使我们能够在不中断实现该接口的类的情况下向接口添加新的功能。让我们来看看下面的例子。
publicclassMyClassimplementsInterfaceA{ /** *@paramargsthecommandlinearguments */ publicstaticvoidmain(String[]args){ //TODOcodeapplicationlogichere } @Override publicvoidsaySomething(){ System.out.println("HelloWorld"); } } interfaceInterfaceA{ publicvoidsaySomething(); }
上面的代码显示了类MyClass实现InterfaceA的方法saySomething()。现在我们给InterfaceA添加一个名为sayHi()的新方法。通过这样做,我们向类MyClass引入一个问题,因为它不会编译,直到我们提供方法sayHi()的实现。
这时Default就有用了。通过在方法的访问修饰符之前添加关键字default,我们不必为类MyClass中的方法sayHi()提供实现。
在最严格的意义上,default是倒退一步,因为它们允许你用代码“污染”你的接口。但它们提供了最优雅和实用的方式来允许向后兼容性。它使jdk更容易更新所有Collections类,并为您改装现有的Lambda代码。(其实这就是default产生最初原因)
publicclassMyClassimplementsInterfaceA{ /** *@paramargsthecommandlinearguments */ publicstaticvoidmain(String[]args){ //TODOcodeapplicationlogichere } @Override publicvoidsaySomething(){ System.out.println("HelloWorld"); } } interfaceInterfaceA{ publicvoidsaySomething(); defaultpublicvoidsayHi(){ System.out.println("Hi"); } }
注意,我们必须提供所有default方法的实现。因此,default方法为我们提供了在接口中实现方法的灵活性。如果具体类不提供该方法的实现,那么实现将被用作默认值。
多接口冲突
由于java中的类可以实现多个接口,可能会有一个情况,其中两个或更多的接口有一个default方法具有相同的名称,因此导致冲突,因为java不知道一次使用什么方法。这将导致编译错误:类MyClass从类型InterfaceA和InterfaceB继承defaultsayHi()
让我们来看看下面的例子。
publicclassMyClassimplementsInterfaceA,InterfaceB{ /** *@paramargsthecommandlinearguments */ publicstaticvoidmain(String[]args){ //TODOcodeapplicationlogichere } @Override publicvoidsaySomething(){ System.out.println("HelloWorld"); } } interfaceInterfaceA{ publicvoidsaySomething(); defaultpublicvoidsayHi(){ System.out.println("HifromInterfaceA"); } } interfaceInterfaceB{ defaultpublicvoidsayHi(){ System.out.println("HifromInterfaceB"); } }
为了解决这样的情况,我们必须在类MyClass中提供sayHi()方法的实现,因此覆盖InterfaceA和InterfaceB中的两个方法。
publicclassMyClassimplementsInterfaceA,InterfaceB{ /** *@paramargsthecommandlinearguments */ publicstaticvoidmain(String[]args){ //TODOcodeapplicationlogichere } @Override publicvoidsaySomething(){ System.out.println("HelloWorld"); } @Override publicvoidsayHi(){ System.out.println("implemetationofsayHi()inMyClass"); } } interfaceInterfaceA{ publicvoidsaySomething(); defaultpublicvoidsayHi(){ System.out.println("HifromInterfaceA"); } } interfaceInterfaceB{ defaultpublicvoidsayHi(){ System.out.println("HifromInterfaceB"); } }
如果我们想要在InterfaceA或InterfaceB中专门调用一个sayHi()方法,我们也可以这样做:
publicclassMyClassimplementsInterfaceA,InterfaceB{ /** *@paramargsthecommandlinearguments */ publicstaticvoidmain(String[]args){ //TODOcodeapplicationlogichere } @Override publicvoidsaySomething(){ System.out.println("HelloWorld"); } @Override publicvoidsayHi(){ InterfaceA.super.sayHi(); } } interfaceInterfaceA{ publicvoidsaySomething(); defaultpublicvoidsayHi(){ System.out.println("HifromInterfaceA"); } } interfaceInterfaceB{ defaultpublicvoidsayHi(){ System.out.println("HifromInterfaceB"); }
总结
以上就是本文关于DefaultMethods实例解析的全部内容,希望对大家能够有帮助。