@SuppressWarnings(“all”)
public class Demo1 /extends Object/{
@Override
public String toString(){
return “”;
}
@Deprecated
public static void testoo1(){
System.out.println(“test.001”);
}
public static void test002(){
List list = new ArrayList<>();
}
public static void main(String[] args) {
Date d = new Date();
testoo1();
}
}
@SuppressWanings
定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数值都是已经定义好了的,我们选择性的使用就好了
参数 说明
Deprecation 使用了过时的类或方法的警告
Unchecked 执行了为检查的转换时的警告,如使用集合时未指定泛型
Fallthrouqh 当在switch语句使用时发生case穿透
Path 在类路径、源文件路径等中有不存在路径的警告
Serial 当在可序列化的类上缺少serialVersionUID定义时的警告
Finally 任何filally子句不能完成时的警告
All 关于以上所有情况的警告
3、自定义注解和元注解
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口
要点:
@interface用来声明一个注解
格式为:public @interface 注解名(定义体)
其中的每一个方法实际上是声名了一个配置参数
方法的名称就是参数的名称
返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)
可以通过default来声明参数的默认值
如果只有一个参数成员,一般参数名为value
package com.lyy.test.annotion;
@SxtMyAnnotation
public class Demo2 {
@SxtMyAnnotation(age=19,studentName=“泡泡”, id=1001,schools={“北京大学”,“孝感学院”})
public void test(){
}
@SxtAnnotation2(“1”)
public void test2(){
}
}
package com.lyy.test.annotion;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtMyAnnotation {
String studentName() default “”;
int age() default 18;
int id() default -1;
String[] schools() default {“清华大学”,“湖北学院”};
}
package com.lyy.test.annotion;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtAnnotation2 {
String value();
}
最常用到的就是@Target和@Retenion两个元注解
**
**
**
**
4、小案例
使用反射读取注解的信息,模拟处理注解信息的流程
package com.lyy.test.annotion;
import java.lang.annotation.Annotation;
/**
-
使用反射读取注解的信息,模拟处理注解信息的流程
-
@author lyy
*/
public class Demo3 {
public static void main(String[] args) {
try {
Class cls = Class.forName(“com.lyy.test.annotion.Student”);
//获取类的所有注解
Annotation[] annction = cls.getAnnotations();
for (Annotation a :annction) {
System.out.println(a);
}
//获取类中指定的注解
Table tab = (Table)cls.getAnnotation(Table.class);
System.out.println(tab.value());
//获得类的属性的注解
java.lang.reflect.Field f = cls.getDeclaredField(“studentName”);
Field fx = f.getAnnotation(Field.class);
System.out.println(fx.columName()+“=“+fx.type()+”=”+fx.length());
//根据获取的表名、字段的信息、拼出DDL语句、然后,使用JDBC执行这个SQL,在数据库中生成相关的表
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.lyy.test.annotion;
@Table(“tb_student”)
public class Student {
@Field(columName=“id”,type=“int”,length=100)
private int id;
@Field(columName=“sname”,type=“varchar”,length=100)
private String studentName;
@Field(columName=“age”,type=“int”,length=3)
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getAge() {
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
面试准备+复习分享:
为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。*
[外链图片转存中…(img-ZoaSOLc2-1710687992608)]
[外链图片转存中…(img-AzFw9dMH-1710687992608)]
[外链图片转存中…(img-Jjp5iXDa-1710687992609)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-t47dVrzk-1710687992609)]
面试准备+复习分享:
为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦
[外链图片转存中…(img-UO0mwCUn-1710687992610)]