Java基础——注解

1 注解

注解是以” @注释名“在代码中存在的,还可以添加一些参数值,例如:

注解Annotation是从JDK5.0开始引入。

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:79a8044d-ece5-4571-a122-1cc79db09ee7

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:f463dab7-ef21-48fd-a50d-07b20949118e

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:45c11870-3edf-4347-81bd-237a5a0d0457

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:34e54db0-23d3-4dc8-9e4d-48cbb74a2109

只能标记在方法上。

它会被编译器程序读取。

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:60a5b23e-16bb-4215-b356-f58f535cabc9

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:c89b6b91-71b9-4288-95ee-8d7dfaa7c359

可以用于修饰 属性、方法、构造、类、包、局部变量、参数。

它会被编译器程序读取。

抑制编译警告。

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:ce938d53-006a-40ea-a752-f0d28256ab8a

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:c6907354-57f1-4934-8d97-90f79c52131e

它会被编译器程序读取。

示例代码:

import java.util.ArrayList;public class TestAnnotation {

JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework),供Java开发人员编写单元测试之用。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。JUnit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。

要使用JUnit,必须在项目的编译路径中必须引入JUnit的库,即相关的.class文件组成的jar包。如何把JUnit的jar添加到编译路径如图所示:

后面会学习maven,在maven仓库中统一管理所有第三方框架和工具组件的jar,但是现在没有学习maven之前,可以使用本地jar包。

第一步:在当前IDEA项目目录下建立junitlibs,把下载的JUnit的相关jar包放进去:

第二步:在项目中添加Libraries库

第三步:选择要在哪些module中应用JUnit库

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:210aa1df-b9cd-4eb9-b0c4-d012de8d07fe

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:ae440252-2213-446f-946e-3c75bc930305

注意Scope:选择Complie,否则编译时,无法使用JUnit。

第5步:下次如果有新的模块要使用该libs库,这样操作即可

JUnit4版本,要求@Test标记的方法必须满足如下要求:

  • 所在的类必须是public的,非抽象的,包含唯一的无参构造的。
  • @Test标记的方法本身必须是public,非抽象,非静态的,void无返回值,()无参数的。
import org.junit.Test;public class TestJUnit {

在idea64.exe.vmoptions配置文件中加入下面一行设置,重启idea后生效。

需要注意的是,要看你当前IDEA读取的是哪个idea64.exe.vmoptions配置文件文件。如果在C盘的用户目录的config下(例如:C:\Users\Irene .IntelliJIdea2019.2\config)也有一个idea64.exe.vmoptions文件,那么将优先使用C盘用户目录下的。否则用的是IDEA安装目录的bin目录(例如:D:\ProgramFiles\JetBrains\IntelliJ_IDEA_2019.2.3\bin)下的idea64.exe.vmoptions文件。

-Deditable.java.test.console=true

1.4 自定义注解

注解是以” @注释名“在代码中存在的,还可以添加一些参数值,例如:

注解Annotation是从JDK5.0开始引入。

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:8585ab1a-5342-4d91-bdb2-156b5e1c8908

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:24323a11-d50e-4389-8f3b-55905d0ad8db

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:16bd0b13-f509-4b68-ad9e-9e15d7917dc3

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:6b89d195-ecdf-496a-b229-45190765abbc

JDK1.5在java.lang.annotation包定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。

(1)@Target:用于描述注解的使用范围

  • 可以通过枚举类型ElementType的10个常量对象来指定
  • TYPE,METHOD,CONSTRUCTOR,PACKAGE…..

(2)@Retention:用于描述注解的生命周期

  • 可以通过枚举类型RetentionPolicy的3个常量对象来指定
  • SOURCE(源代码)、CLASS(字节码)、RUNTIME(运行时)
  • ==唯有RUNTIME阶段才能被反射读取到==。

(3)@Documented:表明这个注解应该被 javadoc工具记录。

(4)@Inherited:允许子类继承父类中的注解

示例代码:

package java.lang;import java.lang.annotation.*;
package java.lang;import java.lang.annotation.*;import static java.lang.annotation.ElementType.*;​
package java.lang;import java.lang.annotation.*;import static java.lang.annotation.ElementType.*;​
【元注解】【修饰符】 @interface 注解名{    【成员列表】}
  • 自定义注解可以通过四个元注解@Retention,@Target,@Inherited,@Documented,分别说明它的声明周期,使用位置,是否被继承,是否被生成到API文档中。
  • Annotation 的成员在 Annotation 定义中以无参数有返回值的抽象方法的形式来声明,我们又称为配置参数。返回值类型只能是八种基本数据类型、String类型、Class类型、enum类型、Annotation类型、以上所有类型的数组
  • 可以使用 default 关键字为抽象方法指定默认返回值
  • 如果定义的注解含有抽象方法,那么使用时必须指定返回值,除非它有默认值。格式是”方法名 = 返回值”,如果只有一个抽象方法需要赋值,且方法名为value,可以省略”value=”,所以如果注解只有一个抽象方法成员,建议使用方法名value。
import java.lang.annotation.*;
import java.lang.annotation.*;

Original: https://www.cnblogs.com/CYan521/p/16401021.html
Author: 再美不及姑娘你
Title: Java基础——注解

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/562344/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球