引子
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