使用Java 8中的默认方法与Abstract类进行接口

本文翻译自:Interface with default methods vs Abstract class in Java 8

As Java 8 allows for default implementation of methods in interface called Default Methods , there seems to be a confusion between when would I use a abstract class . 由于Java 8允许在称为“ 默认方法”的接口中默认实现方法 ,因此在何时使用abstract class之间似乎存在混淆。

So when should interface with default methods be used and when should an abstract class be used? 那么什么时候应该使用默认方法接口,什么时候应该使用抽象类呢? Are the abstract classes still useful in that scenario? 在这种情况下,抽象类仍然有用吗?


#1楼

参考:https://stackoom.com/question/1LuVi/使用Java-中的默认方法与Abstract类进行接口


#2楼

This is being described in this article . 这是这所描述的文章 Think about forEach of Collections. 想想forEach集合。

List<?> list = …
list.forEach(…);

The forEach isn't declared by java.util.List nor the java.util.Collection interface yet. java.util.Listjava.util.Collection接口都没有声明forEach。 One obvious solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. 一个显而易见的解决方案是将新方法添加到现有接口中,并在JDK中要求的地方提供实现。 However, once published, it is impossible to add methods to an interface without breaking the existing implementation. 但是,一旦发布,就不可能在不破坏现有实现的情况下向接口添加方法。

The benefit that default methods bring is that now it's possible to add a new default method to the interface and it doesn't break the implementations. 默认方法带来的好处是,现在可以在接口中添加新的默认方法,并且不会破坏实现。


#3楼

There are a few technical differences. 有一些技术差异。 Abstract classes can still do more in comparison to Java 8 interfaces: 与Java 8接口相比,抽象类仍然可以做更多的事情:

  1. Abstract class can have a constructor. 抽象类可以具有构造函数。
  2. Abstract classes are more structured and can hold a state. 抽象类更加结构化,可以保存一个状态。

Conceptually, main purpose of defender methods is a backward compatibility after introduction of new features (as lambda-functions) in Java 8. 从概念上讲,防御程序方法的主要目的是在Java 8中引入新功能(如lambda函数)后向后兼容。


#4楼

There's a lot more to abstract classes than default method implementations (such as private state), but as of Java 8, whenever you have the choice of either, you should go with the defender (aka. default ) method in the interface. 除了默认方法实现(例如私有状态)外,抽象类还有很多,但是从Java 8开始,无论选择哪种方法,都应该在接口中使用Defender(又名default )方法。

The constraint on the default method is that it can be implemented only in the terms of calls to other interface methods, with no reference to a particular implementation's state. 对默认方法的约束是,只能在对其他接口方法的调用方面实现默认方法,而无需参考特定实现的状态。 So the main use case is higher-level and convenience methods. 因此,主要用例是更高级别的便捷方法。

The good thing about this new feature is that, where before you were forced to use an abstract class for the convenience methods, thus constraining the implementor to single inheritance, now you can have a really clean design with just the interface and a minimum of implementation effort forced on the programmer. 这项新功能的好处在于,在您不得不为便利方法使用抽象类,从而将实现者限制为单一继承之前,现在您可以拥有仅接口和最少实现的真正干净的设计。程序员的努力。

The original motivation to introduce default methods to Java 8 was the desire to extend the Collections Framework interfaces with lambda-oriented methods without breaking any existing implementations. 向Java 8引入default方法的最初动机是希望在不破坏任何现有实现的情况下使用面向lambda的方法扩展Collections Framework接口。 Although this is more relevant to the authors of public libraries, you may find the same feature useful in your project as well. 尽管这与公共图书馆的作者更为相关,但是您可能会发现相同的功能在您的项目中也很有用。 You've got one centralized place where to add new convenience and you don't have to rely on how the rest of the type hierarchy looks. 您可以在一个集中的地方添加新的便利,而不必依赖其他类型层次结构的外观。


#5楼

These two are quite different: 这两个完全不同:

Default methods are to add external functionality to existing classes without changing their state. 默认方法是在不更改其状态的情况下向现有类添加外部功能

And abstract classes are a normal type of inheritance, they are normal classes which are intended to be extended. 抽象类是继承的普通类型,它们是打算扩展的普通类


#6楼

As described in this article, 正如在描述这个文章,

Abstract classes versus interfaces in Java 8 Java 8中的抽象类与接口

After introducing Default Method, it seems that interfaces and abstract classes are same. 引入默认方法后,接口和抽象类似乎相同。 However, they are still different concept in Java 8. 但是,它们仍然是Java 8中的不同概念。

Abstract class can define constructor. 抽象类可以定义构造函数。 They are more structured and can have a state associated with them. 它们更加结构化,并且可以具有与之关联的状态。 While in contrast, default method can be implemented only in the terms of invoking other interface methods, with no reference to a particular implementation's state. 相比之下,默认方法只能在调用其他接口方法方面实现,而无需参考特定实现的状态。 Hence, both use for different purposes and choosing between two really depends on the scenario context. 因此,两者用于不同目的以及在两者之间进行选择实际上取决于场景上下文。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值