Java 5 Annotation

本文介绍了如何使用Java注解来标记需要完成的任务,并通过实例展示了不同类型的注解应用,包括简单的标记注解和带有成员变量的复合注解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Interface is similar to Interface just it is used as templates for documentation, as a standard; especially useful for team development. However, it's pain on the butt - making the coding so difficult to read.

Example 1:

@interface - declaration

package com.myannotation.examples;

/**
 * Marker annotation to indicate that a method or class
 *   is still in progress.
 */
public @interface InProgress { }
@interface - using
@com.myannotation.examples.InProgress
public void calculateInterest(float amount, float rate) {
  // Need to finish this method later
}

@interface - add a member

package com.myannotation.examples;

/**
 * Annotation type to indicate a task still needs to be
 *   completed.
 */
public @interface TODO {
  String value();
}
@interface - using with a member

@com.myannotation.examples.InProgress
@TODO("Need to figure out my debt")
public void calculateMyDebt(float amount, float rate) {
  // Need to finish this method later
}

or

@com.myannotation.examples.InProgress
@TODO(value="Need to figure out my debt")
public void calculateMyDebt(float amount, float rate) {
  // Need to finish this method later
}

Example 2: A little bit complicate case

package com.myannotation.examples;

public @interface GroupTODO {

  public enum Severity { CRITICAL, IMPORTANT, TRIVIAL, DOCUMENTATION };

  Severity severity() default Severity.IMPORTANT;
  String item();
  String assignedTo();
  String dateAssigned();
}
@com.myannotation.examples.InProgress
@GroupTODO(
    item="Need to figure out my debt",
    assignedTo="chefZ",
    dateAssigned="08/04/2007"
)
public  void calculateMyDebt(float amount, float rate) {
    // Need to finish this method later
}
@com.myannotation.examples.InProgress
@GroupTODO(
    severity=GroupTODO.Severity.DOCUMENTATION,
    item="Need to explain why I have to pay",
    assignedTo="chefZ",
    dateAssigned="08/04/2008"
)
public  void reallyConfusedPayment(int codePoint) {
    // Really weird payment  }
If someone expect me to enjoy coding this, he must have some nerds. :-) However, more to come.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值