分类: Java
2010-07-13 21:06:13
Hist:
1007013, draft
Two kinds of interfaces: normal interfaces and annotation types.
Interface Declarations
InterfaceDeclaration:
NormalInterfaceDeclaration
AnnotationTypeDeclaration
NormalInterfaceDeclaration:
InterfaceModifiersopt interface Identifier
TypeParametersopt ExtendsInterfacesopt InterfaceBody
Interface Modifiers
InterfaceModifiers:
InterfaceModifier
InterfaceModifiers
InterfaceModifier
InterfaceModifier: one of
Annotation public
protected private abstract static strictfp
abstract Interfaces
Every interface is implicitly abstract.
strictfp interfaces
Generic Interfaces and Type Parameters
Superinterfaces and Subinterfaces
Interface Body and Member Declarations
Access to Interface Member Names
All interface members are implicitly public.
Interface Members
Field(Constant) Declarations
ConstantDeclaration:
ConstantModifiersopt Type
VariableDeclarators ;
ConstantModifiers:
ConstantModifier
ConstantModifier
ConstantModifers
ConstantModifier: one of
Annotation public static
final
Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these medifiers for such fields.
Initialization of Fields in Interfaces
Every field in the body of an interface must have an initialization expression, which need not be a constant expression. The variable initializer is evaluted and the assignment performed exactly once, when the interface is initialized.
If two fields with the same name are inherited by an interface, then a single ambiguous member results.
If a single field is inherited multiple times from the same interface, then only a single member results.
Inheritance and Overriding
An instance method m1 declared in an interface I overrides another instance method m2, declared in interface J iff both of the following are true:
1) I is a subinterface of J
2) the signature of m1 is a subsignature of the signature of m2.
Moreover, a method declaration must not have a throws clause that conflicts with that of any method that it overrides, otherwise, a compile-time error occurs.
Overloading
Member Type Declaration
A member type declaration in an interface is implicitly static and public.
Annotation Type(标注类型)
An annotation type declaration is a special kind of interface declaration. To distinguish an annotation type declaration from an ordinary interface declaration, the keyword interface is preceded by an at sign(@).
AnnotationTypeDeclaration:
InterfaceModifiersopt @ interface Identifier AnnotationTypeBody
AnnotationTypeBody:
{
AnnotationTypeElementDeclarationsopt }
AnnotationTypeElementDeclarations:
AnnotationTypeElementDeclaration
AnnotationTypeElementDeclarations
AnnotationTypeElementDeclaration
AnnotationTypeElementDeclaration:
AbstractMethodModifiersopt Type Identifier ( ) DefaultValueopt ;
ConstantDeclaration
ClassDeclaration
InterfaceDeclaration
EnumDeclaration
AnnotationTypeDeclaration
;
DefaultValue:
default ElementValue
The Identifier in an annotation type declaration specifies the name of the annotation type.
Predefined Annotation Types(预定义的标注类型)
Several annotation types are predefined in the libraries of the Java Platform.
Target
annotation.Target is intended to be used in meta-annotations that indicate the kind of program element that an annotation type is applicable to.
Retention
Annotations may be present only in the src code, or they may be present in the binary form of a class or interface. An annotation that is present in the binary may or may not be available at run-time via the reflective libraries of the Java platform.
Inherited
annotation.Inherited is used to indicate that annotations on a class C corresponding to a given annotation type are inherited by subclasses of C.
Override
Programmers occasionally overload a method declaration when they mean to override it.
SuppressWarnings
SuppressWarnings supports programmer control over warnings otherwise issued by the Java compiler.
Deperecated
A program element annotated @Deprecated is one that programmers are discouraged for using.
Annotations(标注)
An annotation is a modifier consisting of the name of an annotation type and zero or more element-value pairs, each of which associates a value with a different element of the annotation type. The purpose of an annotation is simply to associate information with the annotated program element.
Annotations must contain an element-value pair for every element of the corresponding annotation type, except for those elements with default values, or a compile-time error occurs.
Annotations are conventionally placed before all other modifiers, but this is not a requirement. They may be freely intermixed with other modifiers.
There are three kinds of annotations. The first(normal annotation) is fully general. The others(marker annotation and single-element annotation) are merely shorthands.
Annotations:
Annotation
Annotations Annotation
Annotation:
NormalAnnotation
MarkerAnnotation
SingleElementAnnotation
A normal annotation is used to annotate a program element:
NormalAnnotation:
@ TypeName ( ElementValuePairsopt )
ElementValuePairs:
ElementValuePair
ElementValuePairs ,
ElementValuePair
ElementValuePair:
Identifier = ElementValue
ElementValue:
ConditionalExpression
Annotation
ElementValueArrayInitializer
ElementValueArrayInitializer:
{ ElementValuesopt ,opt }
ElementValues:
ElementValue
ElementValues ,
ElementValue
TypeName names the annotation type corresponding to the annotation. The Identifier in an ElementValuePair must be the simple name of one of the elements of the annotation type indentified by TypeName in the containing annotation. An ElementValueArrayInitializer is similar to a normal array initializer, except that annotations are permitted in place of expressions.
An annotation on an annotation type declaration is known as a meta-annotation. An annotation type may be used to annotate its own declaration.
The second form of annotation, marker annotation, is a shorthand designed for use with marker annotation types:
MarkerAnnotation:
@ TypeName
The third form of annotation, single-element annotation, is a shorthand designed for use with single-element annotation types:
SingleElementAnnotation:
@ TypeName (
ElementValue )
REF
1. JLS 3nd