Learn from teacher Wang Annotations (ii) Classification of annotations and their built-in annotations

Source: Internet
Author: User
Tags deprecated

Learn from teacher Wang Note (ii): Classification of annotations and built-in annotations
Teacher: Wang Shaohua QQ Group No.: 4,837,736,641, the classification of annotations

In Java, annotations can be divided into 3 categories, based on the use and use of annotations, respectively

Built-in annotations (also known as basic annotations), defined under the Java.lang package

Meta-annotations (meta Annotation)

Custom annotations

Second, the built-in annotation (a) classification

3 standard annotation types are available under the Java.lang package for JDK5.0 and above, respectively

@Override:

@Deprecated

@SuppressWarnings

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/93/wKiom1dbjmLz_Ot7AABQofevZKQ995.png " data_ue_src= "E:\My knowledge\temp\04dc15f5-de04-4402-b260-b1ccda341efd.png" >

(b) @Override

@Override is used as a method of labeling, which illustrates the method by which the overridden method overrides the parent class.

1, it is very simple to use, as long as the overridden subclass method before adding @override, as shown in the following code

12345 publicclass Fruit {    public voidgetObjectInfo(){        System.out.println("水果的getObjectInfo方法");    }}
123456 publicclass Apple extends Fruit {    @Override    public voidgetObjectInfo() {        System.out.println("苹果重写水果的getObjectInfo方法");    }}

2, if the method is not to rewrite the parent class, and the use of @override annotations, will be compiled error

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/92/wKioL1dbj2-QyWWsAAANtWkSHvs039.png " data_ue_src= "E:\My knowledge\temp\43a6ffa6-9449-48dd-8d39-aa91a8a3f2ce.png" >

Therefore, @override is often used to prevent the method name from being misspelled when overriding the parent class method.

3, in addition, special attention, @Override can only be used to modify the method, but not to decorate other program elements!

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/93/wKiom1dbjmKAzZasAAAKKTp2H7Y342.png " data_ue_src= "E:\My knowledge\temp\c84bdbb5-10fd-4370-a92f-d45f5dd39b68.png" >

(iii) @Deprecated annotations

Used to indicate that a program element (class, method, member variable, and so on) is obsolete and the compiler will no longer like the use of this labeled program element.

If used (either the current class or a class under another package), compilation draws a slash on the program element to indicate that the program element is obsolete.

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/92/wKioL1dbj3Dh80sXAAAdPIdRrOM268.png " data_ue_src= "E:\My knowledge\temp\0e1f84ee-5378-453b-b22d-cf21e47f41ed.png" >

(iv), @SuppressWarning Note 1, @SuppressWarning annotations

@SuppressWarnings annotations indicate blocking compiler warnings, which are used to selectively turn off the compiler's warnings for program elements such as classes, methods, and member variables, and their child elements.

@SuppressWarnings will always work on all child elements of the program element

1234567891011121314 @SuppressWarnings({"unchecked","rawtypes"})public class Apple extends Fruit {        @Override    public void getObjectInfo() {        System.out.println("苹果重写水果的getObjectInfo方法");    }        public void printObject(){        Apple apple = new Apple();        List list = new ArrayList();        list.add(apple);    }}

@SuppressWarnings ("unchecked") annotations to identify the Apple class cancellation type check compiler warning

Rawtypes: is a note from Eclipse 3.6 that passes a parameter with a generic type when the parameter is passed

2. The compiler cancels the corresponding warning when the value in parentheses after @suppresswarnings is the following parameter

Deprecation: Compiler cancels warnings that use outdated program elements

Unchecked: Canceled non-checked conversions

Unused: Canceling a warning that a program element is not being used

Fallthrough: Cancels a warning when the SWITHC block directly leads to the next situation without a break

Path: Warning when a nonexistent path exists in the classpath, source file path, and so on

Serial: Suppress a warning when a SERIALVERSIONUID definition is missing on a serialized class

Finally: suppress a warning when a finally clause does not complete properly

All: Suppress warnings for all situations

3, the use of @suppresswarnings parameters

When there is only one value member variable in the annotation type, the value of a value member variable can be specified directly in parentheses after the annotation, without using the name=value structure pair.

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/82/93/wKiom1dbjmOCJZHcAAAO_74vscA684.png " data_ue_src= "E:\My knowledge\temp\ee1298bd-5c60-4f02-8099-ca986d2af244.png" >

When there are multiple value member variables in the annotation type, you can use curly braces

1234 @SuppressWarnings({"serial","unchecked"})publicclass Apple extends Fruit implementsSerializable{...}
Iii. syntax of annotations

Use annotations with an "@" symbol in front of them, and use annotations as modifiers.

1 @+AnnotationName+(..逗号分割的多个name..)

Where value must be a compile-time, inline annotation, or array. If the annotation type defines a default value for a name, the structure can be omitted for the argument.

(i) Annotations without parameters
1234 @OverridepublicvoidgetObjectInfo() {    System.out.println("苹果重写水果的getObjectInfo方法");}
(ii) annotations with one parameter
123456 @SuppressWarnings(value="unused")publicstatic void main(String[] args) {    Apple apple = newApple();    apple.printObject();    List<Apple> apples;}
(iii) annotations with multiple parameters
123 @SuppressWarnings({"serial","unchecked"})publicclass Apple extends Fruit implementsSerializable{}













From for notes (Wiz)

Learn from teacher Wang Annotations (ii) Classification of annotations and their built-in annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.