Annotation

@Override

有可能出错把toString写成tostring,但编译是不会出错

package cn.fovegage.prac;
 class Book{
     public String tostring(){
         return "书";
     }
 }
public class FanXing {
    public static void main(String []args){
        System.out.println(new Book());
    }
}
----------------------------------
cn.fovegage.prac.Book@659e0bfd

使用Override

package cn.fovegage.prac;
 class Book{
     @Override
     public String tostring(){    //提示出错
         return "书";
     }
 }
public class FanXing {
    public static void main(String []args){
        System.out.println(new Book());
    }
}

正确表达

package cn.fovegage.prac;
 class Book{
     @Override
     public String toString(){
         return "书";
     }
 }
public class FanXing {
    public static void main(String []args){
        System.out.println(new Book());
    }
}

@Deprecated(声明过期操作) fun

package cn.fovegage.prac;
 class Book{
     @Deprecated
     public void ~~fun()~~{
        
     }
 }
public class FanXing {
    public static void main(String []args){
        Book b = new Book();
        b. ~~fun~~ ();
    }
}

@SuppressWarnings(压制警告)

正常代码

package cn.fovegage.prac;
 class Book{
     private T title;
     public void setTitle(T title) {
        this.title = title;
    }
     public T getTitle() {
        return title;
    }
 }
public class FanXing {
    public static void main(String []args){
        Book b = new Book();
        b.setTitle("Java");
    }
}

非正常代码

package cn.fovegage.prac;
 class Book{
     @SuppressWarnings("unused")
    private T title;
     public void setTitle(T title) {
        this.title = title;
    }
 }
public class FanXing {
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void main(String []args){
        Book b = new Book();
        b.setTitle("Java");
    }
}

你可能感兴趣的:(Annotation)