【发布时间】:2015-06-15 08:59:37
【问题描述】:
Java 的内置库 (JDK) 中是否有包含常量字段的接口示例?
从documentation,可以在接口中定义常量声明,但我不记得见过这样的。
public interface OperateCar {
// constant declarations, if any
...
}
【问题讨论】:
Java 的内置库 (JDK) 中是否有包含常量字段的接口示例?
从documentation,可以在接口中定义常量声明,但我不记得见过这样的。
public interface OperateCar {
// constant declarations, if any
...
}
【问题讨论】:
例如
package java.text;
public interface CharacterIterator extends Cloneable
{
/**
* Constant that is returned when the iterator has reached either the end
* or the beginning of the text. The value is '\\uFFFF', the "not a
* character" value which should not occur in any valid Unicode string.
*/
public static final char DONE = '\uFFFF';
但一般来说,在 JDK 接口中很难找到常量,因为它不符合语言约定。
【讨论】:
interface 的每个字段都隐含为public static final,因此使其成为常量。
所以:
public interface MyInterface {
String FOO = "foo";
}
... 等同于:
public interface MyInterface {
public static final String FOO = "foo";
}
【讨论】: