最后
分布式技术专题+面试解析+相关的手写和学习的笔记pdf
还有更多Java笔记分享如下:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Desc {
String value ();
String remark () default "";
}
(2)定义一个类,类的属性上使用该注解
package com.zyq.consts;
import com.zyq.annotation.Desc;
/**
-
用户状态
-
@author zyq
-
@date 2019-04-22
*/
public class UserStatus {
@Desc(value = "正常")
public static int NORMAL = 1;
@Desc(value = "非正常", remark="该状态用户不能登录")
public static int ABNORMAL = 2;
}
(3)定义一个与注解属性对应的类
package com.zyq.vo;
public class FixedVo {
public int key;
public String value;
public String remark;
public int getKey() {
return key;
}
public String getValue() {
return value;
}
public String getRemark() {
return remark;
}
public void setKey(int key) {
this.key = key;
}
public void setValue(String value) {
this.value = value;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "FixedVo [key=" + key + ", value=" + value + ", remark="
+ remark + "]";
}
}
(4)创建一个注解工具类,提供解析注解的方法
package com.zyq.tools;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import com.zyq.annotation.Desc;
import com.zyq.vo.FixedVo;
public class AnnotationTool {
/**
* 获取打了Desc注解的字典属性列表
* @param t 类
* @return 字典属性列表
*/
public static <T> List<FixedVo> getFixedVoList(Class<T> c) {
if (c == null) {
return Collections.emptyList();
}
try {
T cls = c.newInstance();
Field[] fields = c.getDeclaredFields();
List<FixedVo> fixedVoList = new LinkedList<FixedVo>();
for (Field field : fields) {
Desc desc = field.getAnnotation(Desc.class);
if (desc != null) {
FixedVo vo = new FixedVo();
vo.setKey(field.getInt(cls));
vo.setValue(desc.value());
vo.setRemark(desc.remark());
fixedVoList.add(vo);
写在最后
很多人感叹“学习无用”,实际上之所以产生无用论,是因为自己想要的与自己所学的匹配不上,这也就意味着自己学得远远不够。无论是学习还是工作,都应该有主动性,所以如果拥有大厂梦,那么就要自己努力去实现它。
最后祝愿各位身体健康,顺利拿到心仪的offer!
由于文章的篇幅有限,所以这次的蚂蚁金服和京东面试题答案整理在了PDF文档里
转存中…(img-6XleDT1w-1715563610808)]