Java 默认情况下,接口变量是最终的和静态的,方法是公共的和抽象的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2635807/
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
interface variables are final and static by default and methods are public and abstract
提问by sap
The question is why it's been decided to have variable as final and static and methods as public and abstract by default.
问题是为什么默认情况下决定将变量作为最终和静态,将方法作为公共和抽象。
Is there any particular reason for making them implicit,variable as final and static and methods as public and abstract.
是否有任何特殊原因使它们隐式,变量为最终和静态,方法为公共和抽象。
Why they are not allowing static method but allowing static variable?
为什么他们不允许静态方法但允许静态变量?
We have interface to have feature of multiple inheritance in Java and to avoid diamond problem. But how it solves diamond problem,since it does not allow static methods.
我们有接口在Java中具有多重继承的特性并避免钻石问题。但是它如何解决钻石问题,因为它不允许静态方法。
In the following program, both interfaces have method with the same name..but while implementing only one we implement...is this how diamond problem is solved?
在下面的程序中,两个接口都有同名的方法……但是我们只实现了一个接口……这就是解决钻石问题的方法吗?
interface testInt {
int m = 0;
void testMethod();
}
interface testInt1 {
int m = 10;
void testMethod();
}
public class interfaceCheck implements testInt, testInt1{
public void testMethod() {
System . out . println ( "m is"+ testInt.m );
System . out . println ( "Hi World!" );
}
}
采纳答案by Eyal Schneider
As I see it, an interface declares a set of abilities that implementors must have. It refers to the "what" more than to the "how"; It is more a specification than an implementation guideline.
在我看来,接口声明了一组实现者必须具备的能力。它指的是“什么”而不是“如何”;它更像是一个规范而不是一个实施指南。
Therefore, methods which are not public are irrelevant in interfaces. Same with non-static data members, which are more related to the specific implementation.
因此,非公共方法在接口中是无关紧要的。与非静态数据成员相同,更多与具体实现相关。
回答by Maurice Perry
I think that non-static variables aren't allowed in interfaces mainly for pragmatic reasons. Multiple inheritance can be more efficient this way and it avoids some ambiguity issues.
我认为主要出于实用原因,接口中不允许使用非静态变量。多重继承可以通过这种方式更有效,它避免了一些歧义问题。
As the name suggests, an interface just a definition of the operations available on an instance, therefore, it doesn't contain any method implementation.
顾名思义,接口只是实例上可用操作的定义,因此,它不包含任何方法实现。
For these reasons, it wouldn't make any sense to allow methods not to be public and variables not to be static.
由于这些原因,允许方法不是公共的和变量不是静态的是没有任何意义的。
That said, I'm not sure it was a good idea to have different defaults for interfaces than for classes.
也就是说,我不确定接口的默认值与类的默认值不同是否是个好主意。
回答by Agoln
As far as why they are their defaults - it's just the language specification. They designed it to be that way so it is.
至于为什么它们是默认值——这只是语言规范。他们把它设计成这样。
They don't allow static methods in interfaces because interfaces aren't suppose to have any functionality. That's just the definition of an interface.
它们不允许在接口中使用静态方法,因为接口不具有任何功能。这只是接口的定义。
Java doesn't have a diamond problem since interfaces don't contain code. If your interface was allowed to have code, then Java wouldn't be able to determine whether it should call testInt's testMethod() or testInt1's testMethod(). Since interfaces don't have code, Java knows that there is only 1 implementation of testMethod with code that it needs to run.
Java 没有钻石问题,因为接口不包含代码。如果您的接口允许有代码,那么 Java 将无法确定它是否应该调用 testInt 的 testMethod() 或 testInt1 的 testMethod()。由于接口没有代码,Java 知道只有 1 个 testMethod 实现和它需要运行的代码。