Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 957|回复: 0

学习Java注解

[复制链接]

该用户从未签到

发表于 2011-9-12 21:55:34 | 显示全部楼层 |阅读模式
    在java中存在三个JAVA内建的注解@Deprecated @Override @SuppressWarnings
@Deprecated表示不建议使用,可以用来为方法和类注解
一、把方法注解
package cn.yangtao.ceshi;
public class Student{
public static void main(){
  
  sayHello();
}
@Deprecated
private static void sayHello() {
  // TODO Auto-generated method stub
  
}
}
在主方法中使用sayHello()时,编译时会出现警告
二、把类注解
package cn.yangtao.ceshi;
@Deprecated
class People{
public static void onClass(){
  System.out.println("在类上添加注解");
}

}
public class Student{
public static void main(){
  
  sayHello();
  People.onClass();
}
@Deprecated
private static void sayHello() {
  // TODO Auto-generated method stub
  
}
}
引用People时,开发工具就会用横线来提醒你出错
@ Override用来标明是覆写
package cn.yangtao.ceshi;
class People{
public void sayHello(){
  System.out.println("在类上添加注解");
}

}
public class StudentDemo extends People{
public static void main(String args[]){
  new StudentDemo().sayHello();
}
@ Override
public void sayHello(){
  System.out.println("已经覆写");
}
}
此时如果将sayHello()改为sayhello()就会出错,因为已经表明了该方法为覆写过的方法,所以方法名要一致
@ SuppressWarnings 用来压制警告
package cn.yangtao.ceshi;
public class StudentDemo {
@SuppressWarnings("deprecation")
public static void main(String args[]){
  
  new StudentDemo().sayHello();
}
@ Deprecated
public void sayHello(){
  System.out.println("已经覆写");
}
}
以下介绍SuppressWarnings的一些参数
1、deprecation 使用了不赞成使用的类或方法时的警告
2、unchecked 执行了未检查的转换时警告
3、fallthrough 当使用switch操作时case后未加入break操作,而导致程序继续执行其他case语句时出现的警告
4、path 当设置一个错误的类路径、源文件路径时出现的警告
5、serial 当在可序列化的类上缺少serialVersionUID定义时的警告
6、fianally 任何finally子句不能正常完成时警告
7、all 关于以上所有情况的警告
我们也可以定义自己的Annotation
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)  ---------------------------->定义该注解的有效区域,还有SOURCE、RUNTIME
public @interface MyAnnotationDemo01 {
String key();----------------------------------------------->用来接收传递进去的值
String value();
}
使用MyAnnotationDemo01
package cn.yangtao.ceshi;
public class StudentDemo {
@SuppressWarnings("deprecation")
@MyAnnotationDemo01(key="hello",value="hello world")            这行是最关键的,记住赋值的方式
public static void main(String args[]){
  
  new StudentDemo().sayHello();
}
@ Deprecated
public void sayHello(){
  System.out.println("已经覆写");
}
}
对于赋值可以有一下几种情况
1、数组型
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
String[] key();

}
赋值方式
package cn.yangtao.ceshi;
public class StudentDemo {
@SuppressWarnings("deprecation")
@MyAnnotationDemo01(key={"hello","world"})
public static void main(String args[]){
  
  new StudentDemo().sayHello();
}
@ Deprecated
public void sayHello(){
  System.out.println("已经覆写");
}
}
2、设置默认型
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
String key() default "hello";

}
如果你使用该注解时没有给注解导入属性,则就会默认使用"hello"
3、可以用枚举来限制取值范围
定义枚举类
package cn.yangtao.ceshi;
public enum Color {
Green,Red,Blue;
}
标明只能给注解传入枚举属性
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
Color key() ;

}
使用注解
package cn.yangtao.ceshi;
public class StudentDemo {
@SuppressWarnings("deprecation")
@MyAnnotationDemo01(key=Color.Blue) 这个时候就只能取枚举中的对象,如果你有很高的控制欲这一招很好使
public static void main(String args[]){
  
  new StudentDemo().sayHello();
}
@ Deprecated
public void sayHello(){
  System.out.println("已经覆写");
}
}
能够设置属性了,要怎么拿到和使用属性呢 ?
答案:反射找到注解所在的方法(如果忘记去看反射)------------>找到该方法上的注解
枚举
package cn.yangtao.ceshi;
public enum Color {
Green,Red,Blue;
}
注解
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)     //  这个地方要设成运行时,否则在取属性的时候会出现空指向异常
public @interface MyAnnotationDemo01 {
Color key() ;
}
取出useAnnotation方法上注解的属性
package cn.yangtao.ceshi;
public class StudentDemo {
@SuppressWarnings("deprecation")
public static void main(String args[]) throws SecurityException, NoSuchMethodException{
  useAnnotation();
  MyAnnotationDemo01 mt=StudentDemo.class.getMethod("useAnnotation", null).
  getAnnotation(MyAnnotationDemo01.class);
  Color c=mt.key();                                                    取出属性的值
  System.out.println(c.name());                                  获得枚举的对象的姓名
}
@MyAnnotationDemo01(key=Color.Blue)                  设置该属性的内容
public  static void useAnnotation() {
  // TODO Auto-generated method stub
  
}

}
枚举中元注解
概念:注解的注解就是元注解
常用的有:
@Target   声明被他标注的注解的作用区域比如 类、方法、属性、包等
@Documented  表示该注解可以被写到文档里面
@Inherited       声明该注解可以被继承
回复

使用道具 举报

该用户从未签到

发表于 2011-9-12 21:56:00 | 显示全部楼层
谢谢楼主分享。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-6-16 20:36 , Processed in 0.368437 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表