Java是否计划使用默认方法(java8)替代抽象类? [英] Does Java have plan that default method (java8) Substitute for Abstract Class?

查看:139
本文介绍了Java是否计划使用默认方法(java8)替代抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java是否计划默认方法替代抽象类
我找不到使用默认方法而不是抽象的真实案例?

Does Java have plan that default method substitute for Abstract Class? I could not find a real case to use default method instead of Abstract?

推荐答案

默认方法无法替代抽象类,因为抽象类可以(并且经常)具有字段。接口只能包含行为,而不能包含状态,由于Java中的多重状态继承(正确或错误地)被认为是邪恶的,因此将来不太可能改变。

Default methods can't substitute abstract classes, as abstract classes can (and often do) have fields. Interfaces can only contain behaviour and not state, which is unlikely to change in the future as multiple inheritance of state in Java is seen (rightly or wrongly) as evil.

他们也可以有 final 方法,这是另一件你无法用默认方法模仿的方法。

They can also have final methods, which is another thing you can't mimic with default methods.

如果有的话,使用默认方法的接口类似于 traits 而不是抽象类,但匹配并不完美。使用接口作为特征是必须非常小心地完成并且知道它们带来的限制。 (例如任何实现类都可以覆盖默认方法,可能会破坏特征。)

If anything, interfaces with default methods resemble traits rather than abstract classes, but the match isn't perfect. Using interfaces as traits is something that has to be done very carefully and knowing the limitations they come with. (Such as any implementing class can override a default method, potentially ruining the trait.)

更多关于此< a href =https://stackoverflow.com/questions/28681737/java-8-default-methods-as-traits-safe>这里。

这篇关于Java是否计划使用默认方法(java8)替代抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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