java三种注释_Java中三种简单注解介绍和代码实例

简单Java注解

JDK5提供的简单注解类型只有3个. 这三个都是用来预防错误或者进行提醒的,分别是:

1.Override

2.Deprecated

3.Suppresswarnings

需要注意,JDK5(另一个说法,Tiger)实际上并没有许多内置注解;相反,它允许核心Java支持注解特性的能力. JSR-175中严格规定它用来定义元数据功能. 需要由程序员编写自定义的注解类型,其他JSR标准也编写了一系列标准注解类型. 下面将用实例来深入讲解这三个简单注解.

Override 注解

Override 注解指明被注解的方法需要覆写超类中的方法.

如果某个方法使用了该注解,却没有覆写超类中的方法(比如大小写写错了,或者参数错了,或者是子类自己定义的方法),编译器就会生成一个错误.

(注意: JRE5中实现接口中的方法时不能使用Override注解,JRE6允许了,很多时候JRE5会报这个错).

示例1演示了覆写注解:

Java注解示例1:

public class Test_Override {

@Override

public String toString() {

return super.toString() + "测试使用 'Override' 注解";

}

}

如果出现方法名字拼写错误会发生什么? 例如,如果你将toString方法改名为"tostring"(全小写),编译时就会得到类似下面这样的出错信息:

Compiling 1 source file to D:tempNew Folder (2)

TestJavaApplication1buildclasses

D:tempNew Folder (2)TestJavaApplication1srctest

myannotationTest_Override.java:24: method does not override

a method from its superclass

@Override

1 error

BUILD FAILED (total time: 0 seconds)

当然,Eclipse就会直接报红叉.现在IDE发展的很好用,初学者不应该去折腾JDK的命令行了.

Deprecated 注解

这个注解表明如果程序调用一个废弃的(Deprecated,废弃的,过时的)元素时,编译器应该显示警告信息. 示例2显示了如何使用Deprecated 注解.

Java注解示例2

首先,创建一个类,并像下面这样将某个方法标明为 废弃:

public class Test_Deprecated {

@Deprecated

public void doSomething() {

System.out.println("测试使用 弃用 注解: 'Deprecated'");

}

}

接着,尝试从另一个类调用这个方法:

public class TestAnnotations {

public static void main(String arg[]) throws Exception {

new TestAnnotations();

}

public TestAnnotations() {

Test_Deprecated t2=new Test_Deprecated();

t2.doSomething();

}

本例中的doSomething()方法被声明为废弃的方法. 因此,一般情况下不应该调用这个方法. 在编译Test_Deprecated.java 文件时是不会有警告消息的. 但在编译 TestAnnotations.java 时编译器就会给出类似这样的警告信息(Eclipse 会有警告):

Compiling 1 source file to D:tempNew Folder

(2)TestJavaApplication1buildclasses

D:tempNew Folder

(2)TestJavaApplication1srctestmyannotation

TestAnnotations.java:27:

warning: [deprecation] doSomething() in

test.myannotation.Test_Deprecated has been deprecated

t2.doSomething();

1 warning

Suppresswarnings 注解

这个注解告诉编译器应该屏蔽带注解的元素和所有子元素的警告信息. 会压制一个元素集和子元素的所有警告信息. 比如,假设你在一个class上使用了Suppresswarnings 注解压住一个警告,在它的一个方法上用Suppresswarnings 注解来压制另一个警告,则两种警告都会在方法级别被压制住. 请参见示例3.

Java注解示例3:

public class TestAnnotations {

public static void main(String arg[]) throws Exception {

new TestAnnotations().doSomeTestNow();

}

@SuppressWarnings({"deprecation"})

public void doSomeTestNow() {

Test_Deprecated t2 = new Test_Deprecated();

t2.doSomething();

}

}

在本例中,使用 @SuppressWarnings压住了示例2中所示的deprecation警告信息. 因为该方法的这类警告被压住了,所以你不会再看到"deprecation"警告.

注意: 在最内层的元素上使用该注解是比较好的. 因此,如果你只想在一个特定的方法上压制一个警告,你应该在方法上标注,而不是在类上使用注解.

元注解(Meta-Annotations,Java注解类型)

元注解,实际上被称为注解的注解,包含四种类型. 分别是:

1.Target

2.Retention

3.Documented

4.Inherited

Target 注解

Target注解表明注解类型适用于哪种目标元素. 它包含下面的枚举类型值:

1.@Target(ElementType.TYPE)  —— 可以适用于任何类的元素

2.@Target(ElementType.FIELD)  —— 只适用于字段或属性

3.@Target(ElementType.METHOD)  —— 只适用于方法的注解

4.@Target(ElementType.PARAMETER)  —— 只适用于方法的参数

5.@Target(ElementType.CONSTRUCTOR) —— 只适用于构造函数

6.@Target(ElementType.LOCAL_VARIABLE) —— 只适用于局部变量

7.@Target(ElementType.ANNOTATION_TYPE) —— 指明声明类型本身是一个注解类型

示例4演示了Target 注解:

Java注解示例4

首先,定义了一个名为Test_Target的注解类型,带上 @Target元注解,如下所示:

@Target(ElementType.METHOD)

public @interface Test_Target {

public String doTestTarget();

}

接下来,创建一个类,它将使用Test_Target注解:

public class TestAnnotations {

public static void main(String arg[]) {

new TestAnnotations().doTestTarget();

}

// 在方法上使用注解,OK.

// 中间也可以不换行,换2行之类,Java忽略多余的换行

@Test_Target(doTestTarget="Hello World !")

public void doTestTarget() {

System.out.printf("Testing Target annotation");

}

}

@Target(ElementType.METHOD) 注解表明该注解类型只能被用来注解方法. 如果你编译这段代码,不会显示警告消息. 但是,如果将这个注解声明到一个字符串变量上,会发生什么呢? 就像下面这样:

public class TestAnnotations {

// 这是错误的做法,编译不会通过,因为注解的Level不对.

// 元注解指明了只能注解方法,就不能用来注解属性

@Test_Target(doTestTarget="Hello World !")

private String str;

public static void main(String arg[]) {

new TestAnnotations().doTestTarget();

}

public void doTestTarget() {

System.out.printf("Testing Target annotation");

}

}

唯一的变化就是注解声明从方法级转向字段级,这是不正确的. 因为你已经定义了注解   @Test_Target 只适用在方法级,如果你尝试编译这个类,你可能会得到这样的错误信息:

"TestAnnotations.java":

D:R_AND_DTestAnnotationsrctestmyannotation

TestAnnotations.java:16:

annotation type not applicable to this kind of declaration at line

16, column 0

@Test_Target(doTestTarget="Hello World !")

^

Error in javac compilation

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用Java编写的模板方法模式的示例代码,并附有注释: ```java // 抽象类,定义模板方法和基本操作方法 abstract class AbstractClass { // 模板方法,定义算法的骨架 public final void templateMethod() { // 调用基本操作方法 operation1(); operation2(); operation3(); } // 基本操作方法,由具体子类实现 protected abstract void operation1(); protected abstract void operation2(); // 默认实现的基本操作方法 protected void operation3() { System.out.println("AbstractClass的operation3默认实现"); } } // 具体子类,实现抽象类的基本操作方法 class ConcreteClass extends AbstractClass { @Override protected void operation1() { System.out.println("ConcreteClass的operation1实现"); } @Override protected void operation2() { System.out.println("ConcreteClass的operation2实现"); } } // 客户端代码 public class Client { public static void main(String[] args) { AbstractClass abstractClass = new ConcreteClass(); // 调用模板方法,执行算法 abstractClass.templateMethod(); } } ``` 在这个示例,抽象类 `AbstractClass` 定义了模板方法 `templateMethod()` 和基本操作方法 `operation1()`、`operation2()`、`operation3()`。其 `templateMethod()` 定义了算法的骨架,通过调用基本操作方法来完成具体的算法逻辑。 具体子类 `ConcreteClass` 继承了抽象类,并实现了基本操作方法。在客户端代码,创建了一个具体子类的实例,并调用了模板方法来执行算法。 注意,模板方法模式通过定义算法的骨架和延迟具体实现到子类,实现了行为的复用和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值