为什么在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));
}
}
其他回答
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));
}
}
Public:用于所有类的可访问性,就像接口中呈现的方法一样
static:作为接口不能有对象,即interfaceName。variableName可以用来引用它,也可以直接在实现它的类中引用variableName。
最后:使它们成为常量。如果两个类实现了相同的接口,并且您赋予它们更改值的权利,那么在var的当前值中就会发生冲突,这就是为什么只允许一次初始化。
而且所有这些修饰符对于接口来说都是隐式的,你真的不需要指定它们中的任何一个。
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
我认为这就是为什么界面变量是静态的原因之一。
不要在接口中声明变量。
接口:系统需求服务。
在接口中,变量默认由public,static,final访问修饰符赋值。 因为:
public:有时候接口可能被放在其他包中。所以它需要从项目中的任何地方访问变量。
static:这样不完整的类不能创建对象。所以在项目中,我们需要访问没有对象的变量,这样我们就可以在interface_filename.variable_name的帮助下访问
final:假设一个接口由许多类实现,并且所有类都试图访问和更新接口变量。这样会导致数据变化不一致,影响到其他类。所以它需要用final声明访问修饰符。
界面是双方之间的契约,是不变的,刻在石头上,因此是最终的。参见契约式设计。
推荐文章
- 在流中使用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