简体   繁体   English

关于java doc中抽象vs接口的令人费解的解释

[英]Puzzling explanation on abstraction vs interface in java doc

In java document, it is said : 在java文档中,有人说:

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. 与接口不同,抽象类可以包含静态和最终的字段,并且它们可以包含已实现的方法。

Is that a correct text? 这是正确的文字吗? that not part confuses me because interfaces don't have static or final fields, right? 属于混淆了我,因为接口没有staticfinal域,对不对?

Source : http://download.oracle.com/javase/tutorial/java/IandI/abstract.html 资料来源: http//download.oracle.com/javase/tutorial/java/IandI/abstract.html

Thanks. 谢谢。

Edit : 编辑:

public interface GroupedInterface extends Interface1,
                                        Interface2, Interface3 {

   // constant declarations
   double E = 2.718282;  // base of natural logarithms

   // method signatures
   void doSomething (int i, double x);
   int doSomethingElse(String s);

}

An interface can contain constant declarations in addition to method declarations. 除了方法声明之外,接口还可以包含常量声明。 All constant values defined in an interface are implicitly public, static, and final. 接口中定义的所有常量值都是隐式public,static和final。 Once again, these modifiers can be omitted. 再一次,可以省略这些修饰符。

Every field declaration in the body of an interface is implicitly public, static, and final. 接口主体中的每个字段声明都是隐式的public,static和final。 It is permitted to redundantly specify any or all of these modifiers for such fields. 允许为这些字段冗余地指定任何或所有这些修饰符。

from section 9.3 of the Java Language Specification ( here ) 来自Java语言规范的9.3节( 这里

Click on " Defining an Interface " on the link in your question: 点击问题链接中的“ 定义界面 ”:

An interface can contain constant declarations in addition to method declarations. 除了方法声明之外,接口还可以包含常量声明。 All constant values defined in an interface are implicitly public, static, and final. 接口中定义的所有常量值都是隐式public,static和final。 Once again, these modifiers can be omitted. 再一次,可以省略这些修饰符。

That is the correct text. 这是正确的文字。

All fields in an interface are inferred to be public, static and final, whether or not explicitly so declared. 无论是否明确声明,接口中的所有字段都被推断为public,static和final。 Just as all methods are public and abstract, whether or not so declared. 正如所有方法都是公开的和抽象的一样,无论是否如此声明。

The quote is correct. 报价是正确的。 Interfaces can have static final fields, but cannot have any other combination (non-static or non-final). 接口可以具有静态最终字段,但不能具有任何其他组合(非静态或非最终)。

Fields on an interface are static and final by default, adding the modifiers is not necessary because there's no alternative. 默认情况下,接口上的字段是静态的和最终的,添加修饰符不是必需的,因为没有其他选择。

For an abstract class it can make sense to give it mutable state, see java.util.AbstractList. 对于抽象类,赋予它可变状态是有意义的,请参阅java.util.AbstractList。 Interfaces are not allowed to have any member that would confer mutable state on a class implementing it. 接口不允许任何成员在实现它的类上赋予可变状态。

思考是..界面内的所有字段都是静态的和最终的,即使你没有写静态和最终!

The documentation is correct. 文档是正确的。 Interfaces may contain static final fields to be used as constants. 接口可能包含要用作常量的静态最终字段。 Abstract classes may contain instance variables to be inherited by extending classes. 抽象类可以包含要通过扩展类继承的实例变量。 Those variables are then available in instances of the extending classes. 然后,这些变量在扩展类的实例中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM