为什么在Java中接口变量默认是静态的和最终的?
当前回答
Just tried in Eclipse, the variable in interface is default to be final, so you can't change it. Compared with parent class, the variables are definitely changeable. Why? From my point, variable in class is an attribute which will be inherited by children, and children can change it according to their actual need. On the contrary, interface only define behavior, not attribute. The only reason to put in variables in interface is to use them as consts which related to that interface. Though, this is not a good practice according to following excerpt:
在Java早期,在接口中放置常量是一种流行的技术,但现在许多人认为这是一种令人讨厌的接口使用,因为接口应该处理对象提供的服务,而不是它的数据。同样,类使用的常量通常是实现细节,但将它们放在接口中会将它们提升到类的公共API。”
我也试过要么放静电要么不放根本没有区别。代码如下:
public interface Addable {
static int count = 6;
public int add(int i);
}
public class Impl implements Addable {
@Override
public int add(int i) {
return i+count;
}
}
public class Test {
public static void main(String... args) {
Impl impl = new Impl();
System.out.println(impl.add(4));
}
}
其他回答
接口可以由任何类实现,如果该值被其中一个实现类改变了,那么就会对其他实现类产生误导。接口基本上是指将两个相互关联但又不同的实体组合在一起。因此,在接口内部声明的变量将隐式是final的,也是静态的,因为接口不能被实例化。
在Java中,接口不允许你声明任何实例变量。使用在接口中声明的变量作为实例变量将返回编译时错误。
你可以声明一个常量变量,使用不同于实例变量的静态final。
Java不允许在接口中定义抽象变量和/或构造函数。解决方案:简单地在你的接口和你的实现之间挂起一个抽象类,它只像这样扩展抽象类:
public interface IMyClass {
void methodA();
String methodB();
Integer methodC();
}
public abstract class myAbstractClass implements IMyClass {
protected String varA, varB;
//Constructor
myAbstractClass(String varA, String varB) {
this.varA = varA;
this.varB = VarB;
}
//Implement (some) interface methods here or leave them for the concrete class
protected void methodA() {
//Do something
}
//Add additional methods here which must be implemented in the concrete class
protected abstract Long methodD();
//Write some completely new methods which can be used by all subclasses
protected Float methodE() {
return 42.0;
}
}
public class myConcreteClass extends myAbstractClass {
//Constructor must now be implemented!
myClass(String varA, String varB) {
super(varA, varB);
}
//All non-private variables from the abstract class are available here
//All methods not implemented in the abstract class must be implemented here
}
如果您确定以后不希望将抽象类与其他接口一起实现,也可以使用没有任何接口的抽象类。请注意,你不能创建一个抽象类的实例,你必须先扩展它。
(“protected”关键字意味着只有扩展类才能访问这些方法和变量。)
斯派罗
由于接口没有直接的对象,访问它们的唯一方法是使用类/接口,因此这就是为什么如果接口变量存在,它应该是静态的,否则外界将无法访问它。现在因为它是静态的,它只能保存一个值,任何实现它的类都可以改变它,因此它将是一团糟。
因此,如果有一个接口变量,它将是隐式静态的,最终的和明显的公共!!
public interface A{
int x=65;
}
public interface B{
int x=66;
}
public class D implements A,B {
public static void main(String[] a){
System.out.println(x); // which x?
}
}
这是解决方案。
System.out.println(A.x); // done
我认为这就是为什么界面变量是静态的原因之一。
不要在接口中声明变量。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap