Java Annotations

Java 的 annotation

  • 以@开头 比如@Override. 
  • 不改变complied program的action
  • annotation不完全是comments 能够改变一段代码compile的方式

下面这段代码因为base class里没有display(int x), 所以是用@Override就会报错。

/* Java program to demonstrate that annotations are
   not barely comments (This program throws compiler
   error because we have mentioned override, but not
   overridden, we haver overloaded display) */
class Base
{
     public void display()
     {
         System.out.println("Base display()");
     }
}
class Derived extends Base
{
     @Override
     public void display(int x)
     {
         System.out.println("Derived display(int )");
     }
 
     public static void main(String args[])
     {
         Derived obj = new Derived();
         obj.display();
     }
}