Java注解,小试牛刀

引子

Android开发的话一般都用过ButterKnife 、Retrofit之流,毕竟都是超流行的框架;

本想对这些东西做个使用小结,但是发现里面用的Java注解本人还挺模糊,所以先决定看一下Java注解这一块;

这文章适合对Java注解一窍不通的小伙伴一起学习;

Java注解Annotation

名词解释也不知道怎么说,先来看看样子。使用Android Studio开发的话,可以直接new一个java注解出来;

路径大概是:new->Java Class->kind中选择Annotation;

随便起个名字,比如MyAnnotation,点击OK就可以创建自己的注解了。

public @interface MyAnnotation {
}

这里可以看到,@interface这个标签,姑且就叫它标签好了;类似class代表类,interface代表接口一样,@interface这个标签代表注解;

下面先看一个真正的,有用的注解,ButterKnife中的BindView

package butterknife;

import android.support.annotation.IdRes;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.CLASS;

/**
 * Bind a field to the view for the specified ID. The view will automatically be cast to the field
 * type.
 * <pre><code>
 * {@literal @}BindView(R.id.title) TextView title;
 * </code></pre>
 */
@Retention(CLASS) @Target(FIELD)
public @interface BindView {
  /** View ID to which the field will be bound. */
  @IdRes int value();
}

emm...看着很棒,除了@inte

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值