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

问题描述

Java 有计划用default method 代替Abstract Class 吗?我找不到使用默认方法而不是抽象方法的真实案例?

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 方法,这是您无法用 default 方法模仿的另一件事.

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

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

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.)

此处了解更多信息.

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

09-01 18:27