Annotation _ built-in Annotation

Source: Internet
Author: User
Tags deprecated

Measure the test taker's understanding about the role of Annotation.

Master the three built-in Annotation Systems

Annotation: a new feature added after JDK1.5. This feature is called the metadata feature. After JDK1.5, it is called the Annotation Feature, that is, adding some program information using the Annotation method.

The java. lang. annotation. Annotation interface is required by all Annotation interfaces.

 

 

 

1. @ Override indicates that the method overwrite is correct. For example, the following code is available:

View plaincopy to clipboardprint? Class Person {
Public String getInfo () {// get information
Return "this is a Person class. ";
}
};
Class Student extends Person {// inherit this class
Public String getinfo () {// override method
Return "this is a Student class. ";
}
};
Public class OverrideAnnotationDemo01 {
Public static void main (String args []) {
Person per = new Student ();
System. out. println (per. getInfo (); // output information
}
};
Class Person {
Public String getInfo () {// get information
Return "this is a Person class. ";
}
};
Class Student extends Person {// inherit this class
Public String getinfo () {// override method
Return "this is a Student class. ";
}
};
Public class OverrideAnnotationDemo01 {
Public static void main (String args []) {
Person per = new Student ();
System. out. println (per. getInfo (); // output information
}
};

At this time, there may be some mistake in writing the method name incorrectly.

View plaincopy to clipboardprint? Class Person {
Public String getInfo () {// get information
Return "this is a Person class. ";
}
};
Class Student extends Person {// inherit this class
@ Override
Public String getinfo () {// override method
Return "this is a Student class. ";
}
};
Public class OverrideAnnotationDemo01 {
Public static void main (String args []) {
Person per = new Student ();
System. out. println (per. getInfo (); // output information
}
};
Class Person {
Public String getInfo () {// get information
Return "this is a Person class. ";
}
};
Class Student extends Person {// inherit this class
@ Override
Public String getinfo () {// override method
Return "this is a Student class. ";
}
};
Public class OverrideAnnotationDemo01 {
Public static void main (String args []) {
Person per = new Student ();
System. out. println (per. getInfo (); // output information
}
}; Using Override annotations ensures proper program execution.

2. @ Deprecated

Annotation annotated with @ Deprecated is not recommended.

View plaincopy to clipboardprint? Class Demo {
@ Deprecated // declare operations not recommended
Public String getInfo (){
Return "this is a Person class. ";
}
};
Public class DeprecatedAnnotationDemo01 {
Public static void main (String args []) {
Demo d = new Demo ();
System. out. println (d. getInfo ());
}
};
Class Demo {
@ Deprecated // declare operations not recommended
Public String getInfo (){
Return "this is a Person class. ";
}
};
Public class DeprecatedAnnotationDemo01 {
Public static void main (String args []) {
Demo d = new Demo ();
System. out. println (d. getInfo ());
}
}; The above program causes compilation errors, but a security warning message appears.

View plaincopy to clipboardprint? @ Deprecated // declare operations not recommended
Class Demo {
Public String getInfo (){
Return "this is a Person class. ";
}
};
Public class DeprecatedAnnotationDemo02 {
Public static void main (String args []) {
Demo d = new Demo ();
System. out. println (d. getInfo ());
}
};
@ Deprecated // declare operations not recommended
Class Demo {
Public String getInfo (){
Return "this is a Person class. ";
}
};
Public class DeprecatedAnnotationDemo02 {
Public static void main (String args []) {
Demo d = new Demo ();
System. out. println (d. getInfo ());
}
}; 3. @ SuppressWarnings

Used to suppress warning information.

Taking the previous generic operation as an example, if the generic type is not specified in the generic type, a security warning is certainly prompted during use.

View plaincopy to clipboardprint? Class Demo <T> {
Private T var;
Public T getVar (){
Return this. var;
}
Public void setVar (T var ){
This. var = var;
}
};
Public class SuppressWarningsAnnotationDemo01 {
Public static void main (String args []) {
Demo d = new Demo ();
D. setVar ("Li Xinghua ");
System. out. println ("content:" + d. getVar ());
}
};
Class Demo <T> {
Private T var;
Public T getVar (){
Return this. var;
}
Public void setVar (T var ){
This. var = var;
}
};
Public class SuppressWarningsAnnotationDemo01 {
Public static void main (String args []) {
Demo d = new Demo ();
D. setVar ("Li Xinghua ");
System. out. println ("content:" + d. getVar ());
}
}; In this case, the Annotation SuppressWarnings can be used to suppress the warning information.

View plaincopy to clipboardprint? Class Demo <T> {
Private T var;
Public T getVar (){
Return this. var;
}
Public void setVar (T var ){
This. var = var;
}
};
Public class SuppressWarningsAnnotationDemo01 {
@ SuppressWarnings ("unchecked ")
Public static void main (String args []) {
Demo d = new Demo ();
D. setVar ("Li Xinghua ");
System. out. println ("content:" + d. getVar ());
}
};
Class Demo <T> {
Private T var;
Public T getVar (){
Return this. var;
}
Public void setVar (T var ){
This. var = var;
}
};
Public class SuppressWarningsAnnotationDemo01 {
@ SuppressWarnings ("unchecked ")
Public static void main (String args []) {
Demo d = new Demo ();
D. setVar ("Li Xinghua ");
System. out. println ("content:" + d. getVar ());
}
}; The above is only to suppress a warning message. Of course, you can also suppress multiple warning messages at the same time, just in the form of an array.

View plaincopy to clipboardprint? @ Deprecated
Class Demo <T> {
Private T var;
Public T getVar (){
Return this. var;
}
Public void setVar (T var ){
This. var = var;
}
};
Public class SuppressWarningsAnnotationDemo02 {
@ SuppressWarnings ({"unchecked", "deprecation "})
Public static void main (String args []) {
Demo d = new Demo ();
D. setVar ("Li Xinghua ");
System. out. println ("content:" + d. getVar ());
}
};
@ Deprecated
Class Demo <T> {
Private T var;
Public T getVar (){
Return this. var;
}
Public void setVar (T var ){
This. var = var;
}
};
Public class SuppressWarningsAnnotationDemo02 {
@ SuppressWarnings ({"unchecked", "deprecation "})
Public static void main (String args []) {
Demo d = new Demo ();
D. setVar ("Li Xinghua ");
System. out. println ("content:" + d. getVar ());
}
};


Through the SuppressWarnings annotation just found, we can find that it is received using the value string array. Therefore, we can also explicitly specify the variable to be received when passing in the annotation parameter.

View plaincopy to clipboardprint? @ Deprecated
Class Demo <T> {
Private T var;
Public T getVar (){
Return this. var;
}
Public void setVar (T var ){
This. var = var;
}
};
Public class SuppressWarningsAnnotationDemo03 {
@ SuppressWarnings (value = {"unchecked", "deprecation "})
Public static void main (String args []) {
Demo d = new Demo ();
D. setVar ("Li Xinghua ");
System. out. println ("content:" + d. getVar ());
}
};
@ Deprecated
Class Demo <T> {
Private T var;
Public T getVar (){
Return this. var;
}
Public void setVar (T var ){
This. var = var;
}
};
Public class SuppressWarningsAnnotationDemo03 {
@ SuppressWarnings (value = {"unchecked", "deprecation "})
Public static void main (String args []) {
Demo d = new Demo ();
D. setVar ("Li Xinghua ");
System. out. println ("content:" + d. getVar ());
}
}; Summary:

1. The three built-in Annotation functions of the system can be found to complete other functions of some code through annotations.

Author: "Han shilei programmer column"

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.