注解的基本使用

1.自定义注解类

1 
// 修饰符 数据类型 数据名 [默认值]
public @interface MethodAnnotation { 2 public String[] authors(); 3 public String bookName(); 4 public double bookPrice() default 100.0;//默认注解可以不写 使用默认值 5 }

public @interface Author {
public String[] authors();
}

public @interface FailedAnnotation {
public String[] authors();
public String bookName();
}

2.注解的使用

1 @Author(authors = {"zhangsan", "lisi", "wangwu"})//数组类型的注解
2 public class Book {
3     @FailedAnnotation(authors = "刘德华", bookName = "史记")//数组类型的只有一个元素可以省略{}
4     private String name;
5     private double price;
6     private String[] authors;
7     @MethodAnnotation(authors = "sima", bookName = "世纪",bookPrice = 88)//带默认值的bookPrice可以不写 如果不写就是用 default 100.0
8     public void buyBook(){};
9 }

 

转载于:https://www.cnblogs.com/MrliBlog/p/11052107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值