Android头条面试题解析:Retrofit使用的注解是哪种,注解的底层是怎样实现的?(持续更新)

public String name();

public String value();

}

在 interface 前面的@符号表名这是一个注解,一旦你定义了一个注解之后你就可以将其应用到你的代码中。

在注解定义中的两个指示@Retention(RetentionPolicy.RUNTIME)和@Target(ElementType.TYPE),说明了这个注解该如何使用。

@Retention(RetentionPolicy.RUNTIME)表示这个注解可以在运行期通过反射访问。如果你没有在注解定义的时候使用这个指示那么这个注解的信息不会保留到运行期,这样反射就无法获取它的信息。

@Target(ElementType.TYPE) 表示这个注解只能用在类型上面(比如类跟接口)。你同样可以把Type改为Field或者Method,或者你可以不用这个指示,这样的话你的注解在类,方法和变量上就都可以使用了。 关于 Java 注解更详细的讲解可以访问 Java Annotations tutorial

类注解


你可以在运行期访问类,方法或者变量的注解信息,下是一个访问类注解的例子:

Class aClass = TheClass.class;

Annotation[] annotations = aClass.getAnnotations();

for(Annotation annotation : annotations){

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

}

你还可以像下面这样指定访问一个类的注解:

Class aClass = TheClass.class;

Annotation annotation = aClass.getAnnotation(MyAnnotation.class);

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

方法注解


下面是一个方法注解的例子:

public class TheClass {

@MyAnnotation(name=“someName”, value = “Hello World”)

public void doSomething(){}

}

你可以像这样访问方法注解:

Method method = … //获取方法对象

Annotation[] annotations = method.getDeclaredAnnotations();

for(Annotation annotation : annotations){

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

}

你可以像这样访问指定的方法注解:

Method method = … // 获取方法对象

Annotation annotation = method.getAnnotation(MyAnnotation.class);

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

参数注解


方法参数也可以添加注解,就像下面这样:

public class TheClass {

public static void doSomethingElse(

@MyAnnotation(name=“aName”, value=“aValue”) String parameter){

}

}

你可以通过 Method对象来访问方法参数注解:

Method method = … //获取方法对象

Annotation[][] parameterAnnotations = method.getParameterAnnotations();

Class[] parameterTypes = method.getParameterTypes();

int i=0;

for(Annotation[] annotations : parameterAnnotations){

Class parameterType = parameterTypes[i++];

for(Annotation annotation : annotations){

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("param: " + parameterType.getName());

System.out.println("name : " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

}

}

需要注意的是 Method.getParameterAnnotations()方法返回一个注解类型的二维数组,每一个方法的参数包含一个注解数组。

变量注解


下面是一个变量注解的例子:

public class TheClass {

@MyAnnotation(name=“someName”, value = “Hello World”)

public String myField = null;

}

你可以像这样来访问变量的注解:

Field field = … //获取方法对象

Annotation[] annotations = field.getDeclaredAnnotations();



for(Annotation annotation : annotations){

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/3638879399c065aab1023d3f9a409d59.png)

![img](https://img-blog.csdnimg.cn/img_convert/5f59daac59eb5764144ea37a9df33754.png)

![img](https://img-blog.csdnimg.cn/img_convert/6d8ea0531b248faad24968ce9380989e.png)

![img](https://img-blog.csdnimg.cn/img_convert/30e5c2f6703a565c6e2ef1e2c82e4c72.png)

![](https://img-blog.csdnimg.cn/img_convert/4f619d1aa0817ed616dedc82583c74c3.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**

**如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)**

![](https://img-blog.csdnimg.cn/img_convert/593ecde341b0d65896d511f0c9b8fb26.jpeg)



**如果你进阶的路上缺乏方向,可以加入我们的圈子和安卓开发者们一起学习交流!**

- Android进阶学习全套手册

  

  ![img](https://img-blog.csdnimg.cn/img_convert/aeb1970c3c32cdb62763b8b44713699d.webp?x-oss-process=image/format,png)

- Android对标阿里P7学习视频

  

  ![img](https://img-blog.csdnimg.cn/img_convert/ac4716297da4cdada772c201113493d8.webp?x-oss-process=image/format,png)

- BATJ大厂Android高频面试题

  

  ![img](https://img-blog.csdnimg.cn/img_convert/5495fcd7c78fcb8dbd59da205a298a1a.webp?x-oss-process=image/format,png)

最后,借用我最喜欢的乔布斯语录,作为本文的结尾:

> 人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。
> 你的时间有限,所以不要为别人而活。不要被教条所限,不要活在别人的观念里。不要让别人的意见左右自己内心的声音。
> 最重要的是,勇敢的去追随自己的心灵和直觉,只有自己的心灵和直觉才知道你自己的真实想法,其他一切都是次要。


**《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》[点击传送门即可获取!](https://bbs.csdn.net/forums/f76c2498e3b04ae99081eaf6e6cf692c)**


  [外链图片转存中...(img-VfLd8BMY-1713802372556)]

最后,借用我最喜欢的乔布斯语录,作为本文的结尾:

> 人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。
> 你的时间有限,所以不要为别人而活。不要被教条所限,不要活在别人的观念里。不要让别人的意见左右自己内心的声音。
> 最重要的是,勇敢的去追随自己的心灵和直觉,只有自己的心灵和直觉才知道你自己的真实想法,其他一切都是次要。


**《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》[点击传送门即可获取!](https://bbs.csdn.net/forums/f76c2498e3b04ae99081eaf6e6cf692c)**

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值