基本概念
Annotion (注解)是一个接口,程序可以通过反射来获取指定程序元素的 Annotion 对象,然后通过 Annotion 对象来获取注解里面的元数据。
根据注解的使用方法和用途,我们可以将 Annotation 分为三类:系统注解,元注解,自定义注解。
系统注解
系统注解,即 JDK 内置的注解,主要有:@Override,@Deprecated,@SuppressWarnnings。
1.@Override
修饰方法时表示该方法覆盖了父类的方法,或实现接口的方法
interface Demo{ public void print(); }public class Test implements Demo{ @Override public void print() { } }
2.@Deprecated
修饰已经过时的方法
3.@SuppressWarnnings
抑制编译器警告,即去除警告。
常见的参数值有:
名称 | 作用 |
---|---|
rawtypes | 表示传参时也要传递带泛型的参数 |
deprecation | 使用了不赞成使用的类或方法时的警告 |
unchecked | 执行了未检查的转换时的警告,例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型 |
fallthrough | 当 Switch 程序块直接通往下一种情况而没有 Break 时的警告; |
path | 在类路径、源文件路径等中有不存在的路径时的警告; |
serial | 当在可序列化的类上缺少 serialVersionUID 定义时的警告; |
finally | 任何 finally 子句不能正常完成时的警告; |
all | 关于以上所有情况的警告。 |
实例如下:
// 抑制单类型@SuppressWarnings("unchecked")public void print() { @SuppressWarnings("rawtypes") List list = new ArrayList(); list.add("a"); }// 抑制多类型@SuppressWarnings({ "unchecked", "rawtypes" })public void print() { List list = new ArrayList(); list.add("a"); }// 抑制所有类型@SuppressWarnings({ "all" })public void print() { List list = new ArrayList(); list.add("a"); }
元注解
元注解的作用就是负责注解其他注解。Java5.0 定义了4 个标准的 meta-annotation 类型,它们被用来提供对其它 annotation 类型作说明。
定义的元注解如下:@Target,@Retention,@Documented,@Inherited。
1.@Target
@Target 定义了 Annotation所修饰的对象范围,具体的修饰范围如下:
public enum ElementType { // 用于描述类、接口(包括注解类型) 或enum声明 TYPE, // 用于描述域(即变量) FIELD, // 用于描述方法 METHOD, // 用于描述参数 PARAMETER, // 用于描述构造器 CONSTRUCTOR, // 用于描述局部变量 LOCAL_VARIABLE, // 用于描述注解类型 ANNOTATION_TYPE, // 用于描述包 PACKAGE }
2.@Retention
@Retention 定义了该 Annotation 被保留的时间长短,即指明了 Annotation 的生命周期。
public enum RetentionPolicy { // 在源文件中有效(编译器要丢弃的注解) SOURCE, // class 文件中有效(默认,编译器将把注解记录在类文件中,但在运行时 VM 不需要保留注解) CLASS, // 在运行时有效(编译器将把注解记录在类文件中,在运行时 VM 将保留注解,因此可以反射性地读取) RUNTIME }
3.@Documented
@Documented 定义 Annotation ,表示某一类型的注解将通过 javadoc 和类似的默认工具进行文档化。
如果类型声明是用 Documented 来注解的,则其注解将成为注解元素的公共 API 的一部分。
4.@Inherited
@Inherited 定义 Annotation ,表示注解类型被自动继承,即一个使用了@Inherited 修饰的annotation 类型被用于一个 class,则这个 annotation 将被用于该class的子类。
使用注解类型注解 class 以外的任何事物,@Inherited 都是无效的。
此元注解仅促成从父类继承注解;对已实现接口的注解无效。
当@Inherited annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层
实例如下:
// 定义注解@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)@Inherited@interface MyAnotation{ public String name(); }// 作用在类上@MyAnotation(name="parent") class Parent{ }// 继承 Parent 类public class Test extends Parent{ public static void main(String[] args) { Class<?> cls = Test.class; // 通过 @Inherited 继承父类的注解 Annotation annotation = cls.getAnnotation(MyAnotation.class); MyAnotation myAnotation = (MyAnotation) annotation; System.out.println(myAnotation.name()); } }// 输出结果:parent(若注释掉注解,返回异常)
自定义注解
1.类注解
// 定义注解@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)@interface MyAnnotation { public String name(); public String age(); }// 调用注解@MyAnnotation(name="cook",age="100")public class Test { public static void main(String[] args) { Class<?> cls = Test.class; // 1.取得所有注解 Annotation[] annotations =cls.getAnnotations(); // 2.取得指定注解 MyAnnotation annotation = (MyAnnotation)cls.getAnnotation(MyAnnotation.class); } }
2.方法注解
// 定义注解@Retention(RetentionPolicy.RUNTIME) // 修改作用范围@Target(ElementType.METHOD)@interface MyAnnotation { public String name(); public String age(); } // 调用注解public class Test { public static void main(String[] args) throws Exception { Class cls = Test.class; Method method = cls.getDeclaredMethod("print", null); // 1.取得所有注解 Annotation[] annotations = method.getDeclaredAnnotations(); // 2.取得指定注解 MyAnnotation annotation = (MyAnnotation)method.getAnnotation(MyAnnotation.class); }
3.参数注解
// 定义注解@Retention(RetentionPolicy.RUNTIME) // 修改作用范围@Target(ElementType.PARAMETER)@interface MyAnnotation { public String name(); public String age(); }public class Test { public static void main(String[] args) throws Exception { Class cls = Test.class; Method method = cls.getDeclaredMethod("print", new Class[]{String.class,String.class}); getAllAnnotations(method); } // 作用在参数上 public void print(@MyAnnotation(name = "cook", age = "100") String name, String age) { } public static void getAllAnnotations(Method method) { Annotation[][] parameterAnnotions = method.getParameterAnnotations(); // 通过反射只能取得所有参数类型,不能取得指定参数 Class[] paraemterTypes = method.getParameterTypes(); int i = 0; for (Annotation[] annotations : parameterAnnotions) { Class paraemterType = paraemterTypes[i++]; for (Annotation annotation : annotations) { if (annotation instanceof MyAnnotation) { MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println(paraemterType.getName()); System.out.println(myAnnotation.name()); System.out.println(myAnnotation.age()); } } } } }
4.变量注解
// 定义注解@Retention(RetentionPolicy.RUNTIME) // 修改作用范围@Target(ElementType.FIELD)@interface MyAnnotation { public String name(); public String age(); }public class Test { // 作用在变量上 @MyAnnotation(name = "cook", age = "100") private String name; public static void main(String[] args) throws Exception { Class cls = Test.class; Field field = cls.getDeclaredField("name"); Annotation[] fieldAnnotions = field.getDeclaredAnnotations(); MyAnnotation annotation = (MyAnnotation) field.getAnnotation(MyAnnotation.class); } }
以上就是09.Java 基础 - 注解的内容,更多相关内容请关注PHP中文网(www.php.cn)!

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

要解决Java应用程序中的平台特定问题,可以采取以下步骤:1.使用Java的System类查看系统属性以了解运行环境。2.利用File类或java.nio.file包处理文件路径。3.根据操作系统条件加载本地库。4.使用VisualVM或JProfiler优化跨平台性能。5.通过Docker容器化确保测试环境与生产环境一致。6.利用GitHubActions在多个平台上进行自动化测试。这些方法有助于有效地解决Java应用程序中的平台特定问题。

类加载器通过统一的类文件格式、动态加载、双亲委派模型和平台无关的字节码,确保Java程序在不同平台上的一致性和兼容性,实现平台独立性。

Java编译器生成的代码是平台无关的,但最终执行的代码是平台特定的。1.Java源代码编译成平台无关的字节码。2.JVM将字节码转换为特定平台的机器码,确保跨平台运行但性能可能不同。

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Atom编辑器mac版下载
最流行的的开源编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)