java 7替代接口中的默认方法(仅在Java 8中) [英] java 7 alternative to default methods in interface (only in Java 8)

查看:80
本文介绍了java 7替代接口中的默认方法(仅在Java 8中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的一个接口类中使用默认方法 - 然后我意识到这只在Java 8中可用 - 我们使用的是Java 7.



在Java 7中实现类似的最佳方法是什么?

解决方案

在接口中使用方法签名,如Java 8中所示。

在实现该接口的抽象类中使用方法默认实现。

在扩展该抽象类的类中使用方法最终实现。



现在你必须要知道它与使用Java 8接口的默认方法仍然不一样,特别是因为你不能扩展多个抽象类,而你可以使用它们的默认方法实现多个接口。 / p>

I wanted to use default methods in one of my interface classes - then I realised that this is only available in Java 8 - and we are using Java 7.

What is the best way of achieving similar in Java 7 ?

解决方案

Have your methods signature in an interface, as in Java 8.
Have your method default implementation in an abstract class implementing that interface.
Have your method final implementation in a class extending that abstract class.

Now you have to be aware that it will still not be the same as having Java 8's interfaces' default method, in particular because you cannot extend multiple abstract class, while you can implement multiple interfaces with their default methods.

这篇关于java 7替代接口中的默认方法(仅在Java 8中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆