Java 接口中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1858380/
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-08-12 23:21:11  来源:igfitidea点击:

Variables in Interface

javainterfacestatic

提问by Sandeep

Why is that a variable used in an Interface is PUBLIC STATIC FINAL? Why "static" in particular?

为什么接口中使用的变量是 PUBLIC STATIC FINAL?为什么特别是“静态”?

采纳答案by Jon Skeet

A field declared in an interface can only be a constant anyway, so why would it depend on which instance you use to access it?

接口中声明的字段无论如何只能是常量,那么为什么它取决于您使用哪个实例来访问它呢?

Putting fields in interfaces is often poor style anyway these days. The interface is meant to reflect the capabilities of classes that implement it - which is completely orthogonal to the idea of a constant. It's certainly a nasty idea to use an interface justto declare a bunch of constants. I do occasionally find it useful to make the interface type expose constants which are simple implementations - so a filtering interface might have "ALLOW_ALL" and "ALLOW_NONE" fields, for example.

无论如何,如今在界面中放置字段通常是糟糕的风格。该接口旨在反映实现它的类的功能 - 这与常量的概念完全正交。这当然使用的接口一个馊主意刚刚宣布了一堆常量。我偶尔会发现让接口类型公开常量(这些常量是简单的实现)很有用 - 例如,过滤接口可能具有“ALLOW_ALL”和“ALLOW_NONE”字段。

I suppose you could conceiveof a scenario where implementing an interface didactually add an instance field to your class - but that would break encapsulation not only in terms of it being implicitly public, but also by specifying part of the implementation instead of the API.

我想你可以设想一个场景,实现一个接口确实向你的类添加了一个实例字段 - 但这不仅会破坏封装,因为它是隐式公开的,而且还会通过指定部分实现而不是 API。

回答by fastcodejava

Because you can not instantiate an interface. Also there cannot be any method body to use a non-static non-final variable.

因为你不能实例化一个接口。也不能有任何方法体来使用非静态非最终变量。

回答by Anon.

Why wouldn't it be static?

为什么不是静态的?

It's a constant associated with the interface, rather than with any particular instance of it.

它是一个与接口相关联的常量,而不是与它的任何特定实例相关联。

回答by Tobias Langner

The main reason I guess is implementation detail of the VM/language.

我猜的主要原因是 VM/语言的实现细节。

If an interface is not allowed to have non-static variables, there's no need to allocate memory for the interface during the creation of the class. There's also no need for special naming/renaming mechanisms in case you inherit variables with the same name. The only thing you need is some table to call the correct functions when the interface is used.

如果接口不允许有非静态变量,那么在创建类的过程中就不需要为接口分配内存。如果您继承具有相同名称的变量,也不需要特殊的命名/重命名机制。您唯一需要的是一些表来在使用接口时调用正确的函数。

In short - it makes the live of the language / VM maintainer easier. If you really want to take a look at multiple inheritance and its pitfalls and traps, read Object Oriented Software Construction by Bertrand Meyer (2nd Edition). Then you understand why the interface needs to be so simple (and yet archives most of the things multiple inheritance does).

简而言之 - 它使语言/VM 维护者的生活变得更容易。如果您真的想了解多重继承及其陷阱和陷阱,请阅读 Bertrand Meyer 撰写的面向对象的软件构造(第 2 版)。然后你就会明白为什么接口需要如此简单(而且还归档了多重继承所做的大部分事情)。

回答by bvdb

An interface is a contractthat defines the interactionbetween objects.

接口是定义对象之间交互的契约

This interaction is defined by the exposed methods, not by the variables. Variables would only describe the internal working, not the interaction.

这种交互是由公开的方法定义的,而不是由变量定义的。变量只会描述内部工作,而不是交互。

Note that variables should never be used for interaction. According to the OOP principle of encapsulation, it would be a crime to let 1 class access a variable of another class directly.

请注意,永远不应将变量用于交互。根据封装的OOP原则,让一个类直接访问另一个类的变量是犯罪。

Constants (e.g.Math.PI) are the only acceptable exception. Since constants are the only kind of variables that can be accessed directly by other classes without violating the principle of encapsulation, all variables in an interface are treated as public static finalvariables (i.e. constants)

常量(例如Math.PI)是唯一可接受的例外。由于常量是唯一一种可以在不违反封装原则的情况下被其他类直接访问的变量,因此接口中的所有变量都被视为public static final变量(即常量)