java 对于接口,不能分配最终字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4438228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:22:43  来源:igfitidea点击:

The final field cannot be assigned, for an interface

javainterface

提问by Matt

I have a class Product and an interface LargeProduct. Product implements LargeProduct.

我有一个 Product 类和一个接口 LargeProduct。产品实现大产品。

LargeProduct has a variable height which has getter and setter methods which have to be implemented in the Product class.

LargeProduct 具有可变高度,它具有必须在 Product 类中实现的 getter 和 setter 方法。

The height variable in LargeProduct is defined like so:

LargeProduct 中的 height 变量定义如下:

public int height = null;

The getter method works fine:

getter 方法工作正常:

public int getHeight() {
    return height;
}

But the setter method does not:

但是 setter 方法不会:

public void setHeight(int height) {
    this.height = height;
}

Errors:

错误:

The final field LargeProduct.height cannot be assigned
The static field LargeProduct.height should be accessed in a static way

I'm not sure which error it's actually giving.. I'm using Eclipse and when I hover it gives the first error, and at the bottom of the window it gives the second error.

我不确定它实际上给出了哪个错误。我正在使用 Eclipse,当我悬停时它给出了第一个错误,而在窗口底部它给出了第二个错误。

回答by Jon Skeet

Interfaces can only include constants, not general-purpose variables. Interfaces should onlycontain constants if they're genuinely relevant to the rest of the interface, and they should use SHOUTY_CASEas per any other constant. It sounds like LargeProductshouldn'thave a constant called height- but instead, your implementation should declare the heightfield.

接口只能包含常量,不能包含通用变量。接口应该包含与接口的其余部分真正相关的常量,并且它们应该SHOUTY_CASE像任何其他常量一样使用。听起来LargeProduct不应该有一个常量被调用height- 但相反,您的实现应该声明该height字段。

Note that interfaces are meant to be APIs, showing the abilities of types. Fields shouldn't be part of the API - they're an implementationdetail. After all, who's to say that you'll write getHeightand setHeightbased on a simple variable? Those methods could query a database, or delegate to an instance of some other type. The consumer of the interface shouldn't know or care about that.

请注意,接口旨在成为 API,显示类型的能力。字段不应该是 API 的一部分——它们是一个实现细节。毕竟,谁又能说你会写getHeight,并setHeight基于一个简单的变量?这些方法可以查询数据库,或委托给其他类型的实例。接口的消费者不应该知道或关心这一点。

For further information about fields in interfaces, I suggest you read section 9.3 of the Java Language Specification:

有关接口中字段的更多信息,我建议您阅读Java 语言规范的第 9.3 节

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

接口主体中的每个字段声明都是隐式公共的、静态的和最终的。允许为此类字段冗余指定任何或所有这些修饰符。

回答by Michael Borgwardt

The height variable in LargeProduct is defined like so:

public int height = null;

LargeProduct 中的 height 变量定义如下:

public int height = null;

Variables defined in interfaces are implicitly staticand final, i.e. constants. That's what the compiler complains about.

接口中定义的变量是隐式staticfinal,即常量。这就是编译器所抱怨的。

You cannot define an instance variable in an interface. Just leave it out - the get and set methods ensure that the classes can be used as intended. The actual variable is an implementation detail if the implementing classes.

您不能在接口中定义实例变量。只需忽略它 - get 和 set 方法确保可以按预期使用这些类。如果实现类,则实际变量是实现细节。

回答by Jigar Joshi

By default fields (member variable) in interface are public static final

默认情况下,接口中的字段(成员变量)是public static final

and you don't have setter for final

而且你没有最终的二传手

回答by Paul McKenzie

You can't assign a value to a final field. Declare height in Product as

您不能为最终字段分配值。在 Product 中声明高度为

private int height;

回答by stecb

Exactly org.life.java! And you cannot modify a static final variable! Because it's the way to determine a constant in Java.

正是 org.life.java!并且您不能修改静态最终变量!因为它是在 Java 中确定常量的方法。