interface增加default method的原因

1.interface一直有个缺陷是,一旦设计好了以后,很多类也实现了它。再想加一个方法是很难的,可能每个子类里面都得实现新的抽象方法。default method,可以增加一个方法,并且给出一个默认的实现。

2.增加default method能够扩展interface的能力。例如Comsumer接口中的andThen方法就是default的,非常有用。

3.在java9,in interface,there are private method also, which can extract some  repeative codes,and make the default method looks short .

 

posted @ 2021-10-10 22:18  lhlcc  阅读(71)  评论(0)    收藏  举报