17---java中Annotation01(系统内置的)

来源:互联网 发布:阿里云服务器能翻墙吗 编辑:程序博客网 时间:2024/06/05 11:04
 

Annotation-----注释
1、@Override:
 举例子:
  class Person{
   public String getInfo(){
     return "这是一个Person类";
    }
  }
  class Student extends Person{
    public String getInfo(){//覆写父类的方法
     return "这是一个Student类";
     }
   }
  public class Demo{
    public static void main(String args[]){
      Person per=new Student();
      System.out.println(per.getInfo());
     }
   }
 以上是正确的覆写父类的方法,但是编写程序中,往往可能因为意外将覆写方法错写,
 这样就不是覆写父类的方法了,会被认为成一个新的方法,这样就会造成得到的结果不是
 想要的结果;
 @Override的作用就是保证程序正确的进行,如果@Override后面跟的方法,不是覆写父类的
  方法的话,就会提示错误,从而避免了当成一个新的方法,而得不到想要的结果;
  class Person{
    public String getInfo(){
      return "这是一个Person类";
     }
   }
  class Student extends Person{
    @Override
    public String getinfo(){//覆写父类的方法
     return "这是一个Student类";
     }
   }
  public class Demo{
    public static void main(String args[]){
      Person per=new Student();
      System.out.println(per.getInfo());
     }
   }
  以上程序就会保存,这样可以发现错误(没有覆盖父类方法),保证程序正确进行;

2、@Deprecated:
 举例子:
  class Student{
    @Deprecated           //声明不建议使用的操作,即不建议使用getInfo这个方法
    public String getInfo(){//覆写父类的方法
     return "这是一个Student类";
     }
   }
  public class Demo{
    public static void main(String args[]){
      Student stu=new Student();
      System.out.println(stu.getInfo());
     }
   }
  这样在编译的时候会提示用户,说用户使用了过时的API;
  @Deprecated还可以使用在类上;
   @Deprecated //声明不建议使用的操作,即不建议使用该类
   class Student{           
     public String getInfo(){//覆写父类的方法
      return "这是一个Student类";
      }
    }
   public class Demo{
     public static void main(String args[]){
       Student stu=new Student();
       System.out.println(stu.getInfo());
      }
    }

3、@SuppressWarnings
 用于压制警告信息:
  以以前的泛型操作为例,在泛型中如果没有指定泛型类型,则使用时肯定会出现安全
   警告;

    class Student<T>{
      private T var;
      public T getVar(){
        return this.var;
       }
      public void setVar(T var){
        this.var=var;
       }
     }
    public class Demo{
      @SuppressWarnings("unchecked")//压制安全信息,如果没此注释,则会提示“未检查***”的安全信息;
              //此处的unchecked就是压制 “未检查***”的安全信息,不让其提示;
      public static void main(String args[]){
        Student stu=new Student();
        stu.setVar("lid");
        System.out.println(stu.getVar());
       }
     }
  如果还有别的“安全提示信息”如因为@Deprecated而出的安全提示信息,如果想把这个安全提示信息
   压制住我们这样写:
    @SuppressWarnings({"unchecked","deprecation"})
    
     @Deprecated
     class Student<T>{
       private T var;
       public T getVar(){
         return this.var;
        }
       public void setVar(T var){
         this.var=var;
        }
      }
     public class Demo{
       @SuppressWarnings({"unchecked","deprecation"})//压制安全信息,如果没此注释,则会提示“未检查***”的安全信息;
               //此处的unchecked就是压制 “未检查***”的安全信息,不让其提示;
       public static void main(String args[]){
         Student stu=new Student();
         stu.setVar("lid");
         System.out.println(stu.getVar());
        }
      }
   
   对于@SuppressWarings可以压制多种安全提示;
   还可以这样写:
    @SuppressWarnings(value={"unchecked","deprecation"})

原创粉丝点击