简体   繁体   English

为什么语言设计师允许界面包含字段?

[英]Why language designers allow interface to contain fields?

As per Item 19 in Effective Java, one must use an interface to only represent a type. 根据有效Java中的第19项,必须使用一种接口来仅表示一种类型。 In this case the interface would contain the methods that form part of the public contract that is exposed by a class (implementing the interface) to the clients 在这种情况下,接口将包含构成类(实现接口)公开给客户端的公共合同的一部分的方法。

If so, why do interfaces support fields in the first place? 如果是这样,为什么接口首先支持字段? Since the fields are implicitly public, static, final, (and hence constants), why did the language designers support having them? 由于字段是隐式的public,static,final(因此是常量),因此语言设计者为什么支持使用它们呢? If they were unsupported then developers would invariably use utils class (with private constructor) to define these constants. 如果它们不受支持,则开发人员将始终使用utils类(带有私有构造函数)来定义这些常量。 The anti pattern of using constants only interfaces could have been automatically avoided 本可以自动避免仅使用常量接口的反模式

I am looking at understanding the reasons for supporting constants in the interface. 我正在了解理解在接口中支持常量的原因。 Are they essential as part of the contract with the client? 作为与客户合同的一部分,它们是否必不可少?

Thanks 谢谢

One reason might be constants which are essential to the interface's contract. 原因之一可能是对于接口合同必不可少的常数。 For instance, consider the following interface: 例如,考虑以下接口:

/**
* Generates a probability distribution on a
* specified interval.
*/
public interface DistributionGenerator{
    /*
    * Gets a probability distribution on the specified
    * interval. The exact distribution is unique to the 
    * implementor of this interface. The distribution will
    * be represented as an array of doubles. The first number
    * in the returned array will be the probability at start
    * The last number will be the probability at end. All numbers
    * in between will be probabilities at evenly spaced 
    * intervals between start and end, with the spacing between values
    * given by the constant INTERVAL
    */
    public double[] getDistribution(double start,double end);

    public static final double INTERVAL = 0.000001;
}

However, as you observed, most of the time you see constants in interfaces it is done to simply expose the constants for their own sake, and this is generally considered bad practice . 但是,正如您所观察到的,大多数时候,您会在接口中看到常量,只是为了自己的利益而公开常量,这通常被认为是不好的做法

Are they essential as part of the contract with the client? 作为与客户合同的一部分,它们是否必不可少?

They are if the constant can be given as a parameter to one or more of the interface methods, or if the constant is a documented special return value of one or more of the interface methods. 如果常量可以作为一个或多个接口方法的参数提供,或者常量是一个或多个接口方法的已记录特殊返回值,则它们是它们。

The use of an interface to hold constants for easy use by a class is an anti-pattern, and there are many articles about that, but it is valid to have constants that are specifically for use by the methods defined in the interface itself. 使用接口来保存常量以方便类使用是一种反模式,关于它的文章很多,但是具有专门供接口本身中定义的方法使用的常量是有效的。

However, using enum types and the new Java 8 Optional will eliminate most use-cases for such special constants, so they are very rarely seen in real life. 但是,使用enum类型和新的Java 8 Optional将消除此类特殊常量的大多数用例,因此在现实生活中很少见到它们。


As an example, the InputStream.read() methods all return -1 for "end of the stream". 例如, InputStream.read()方法都为“流的末尾”返回-1 Sure, it is well documented, but wouldn't use of the methods be better self-documented if InputStream had defined a END_OF_STREAM constant for -1 ? 当然,它已经被很好地证明了,但是如果InputStream-1定义了一个END_OF_STREAM常量, 使用这些方法是否会更好地自我证明?

while ((len = inputStream.read(bytes)) != InputStream.END_OF_STREAM) {

// or using a static import:
while ((len = inputStream.read(bytes)) != END_OF_STREAM) {

Ok, well, InputStream is an abstract class, not an interface, though it should have been, so not a perfect example, but it illustrates the point I was trying to make. 好的,好吧, InputStream是一个抽象类,而不是一个接口,尽管它本来应该是,所以不是一个完美的例子,但是它说明了我试图提出的观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM