Introduction:
I’m just going to talk a little bit about annotations, and we’ll talk a little bit more about customizing annotations when we talk about reflection.
The main content
- The role of the Annotation
- The use of three built-in annotations
The specific content
In fact, the software program has gone through three development processes:
- First procedure: Write all configuration related content directly into the code.
- The second process: the configuration is independent of the program code, and the program operates according to the configuration file at runtime.
- Most serious problem: there are too many configuration files in a project to search for errors.
- Third process: Configuration information is useless to the user, and random modification can lead to errors in the program, so you can write the configuration information back into the program, but use some obvious markers to distinguish configuration information from the program.
Annotations are one of JDK1.5’s biggest features. They use annotations to implement different functions of an application. JavaSE supports the development of custom annotations and uses three of the most commonly used base annotations: @Override, @deprecated, and @Suppresswamings.
Exact Override: @override
The toString() method must be overridden if you want to output an object that returns the desired content.
** Use @override
public class Book {
@Override
public String toString(a) {
return "It's a book!; }}Copy the code
The error code
public class Book {
@Override
public String tostring(a) { // Because S is lowercase and not overridden, @override is an error
return "It's a book!; }}Copy the code
So in order to tell the compiler that toString() should be an Override method, we can add @override to explicitly tell the compiler that the method is an Override and that an error will be reported if it is not.
Declare an expired operation @deprecated
For example, if you have a class that has a fun() method and it turns out that fun is a bit inadequate, the developer has two options:
- Option 1: Simply cancel the fun() method and give the new fun2 method.
- Option 2: Save fun() in the new version of the development kit, but somehow tell the user that fun() has some problems, and make fun2() available to the developer.
You can use “@deprecated” for this declaration.
** Examples: ** uses @deprecated
public class Book {
@Deprecated
public void fun(a) {}}public class TestDemo {
public static void main(String args[]) {
Book book = newBook(); book.fun(); }}Copy the code
In the compiler, the fun() method is marked with a dash. Using this operation can be a good way to achieve the function of the old and new.
Suppression warning: @Suppresswamings
Multiple warnings can be suppressed.
** Watch warning
public class Book<T> {
private T title;
public void setTitle(T title) {
this.title = title; }}public class TestDemo {
public static void main(String args[]) {
Book book = new Book(); // There is a warning
book.setTitle("Hello"); // There is a warning}}Copy the code
If the developer left the warning on purpose now, but doesn’t want to keep the warning, you can choose to suppress the warning.
** * Suppress warning
public class Book<T> {
private T title;
public void setTitle(T title) {
this.title = title; }}public class TestDemo {
@SuppressWarnings("rawtypes", "unchecked")
public static void main(String args[]) {
Book book = new Book(); // There is a warning
book.setTitle("Hello"); // There is a warning}}Copy the code
The warning message is now effectively cancelled.
conclusion
Understand the three Java Annotation functions.