For traditional programmers, the most important thing in the annotation is the override annotation. The annotations here refer to declarations that can only be used in methods,
She indicates that the annotated method is used to overwrite a declaration of the parent class. If you stick to this annotation, it can prevent illegal errors of a major category.
<span style="font-size:14px;">public class Bigram{private final char first;private final char second;public Bigram (char first ,char second){this.first=first;this.second=second;}public boolean equals(Bigram b){return b.first==first && b.second==second;}public int hashCode(){return 31*first+second;}public static void main (String args[]){.....s.add(new Bigram(ch,ch));}}</span>
This Code does not get the expected result because the bigram class creator originally wanted to overwrite the equals method and overwrite the hashcode method at the same time.
However, it is unfortunate that the equals method is overloaded. (The equals method must be passed into the object class.) This equals method tests the object's identity, just like =
The operators are the same. This is why you cannot get the desired result.
If we add override to reload the parent class method identifier when writing code, this type of error can be avoided.
In today's ide, the override annotation check function is provided, which enables you to find this method problematic during compilation.
<span style="font-size:14px;">public boolean equals(Bigram b){return b.first==first && b.second==second;}</span>
In short, remember to use the override annotation, the compiler will help you prevent a large number of errors, but there is an exception. Note not necessary in the detail class
Are you sure you want to override the abstract method declaration method.
[code optimization] Stick to the override annotation