含义
基本方法
Class<User> clazz = User.class //通过类的.class文件获取到Class对象 clazz.getName() //类的全限定名 clazz.getSimpleName() //类的名字 clazz.getSuperclass() //类的父类 clazz.getInterfaces() //类实现的接口 clazz.getFields() //类的字段(public修饰的属性,不包含继承) clazz.getDeclaredFields() //类的字段(类中定义的所有属性,不包含继承) field.getType() //字段的类型 属性类型 field.getName() //字段名 属性名 field.set(obj,value) //对已有对象的该属性进行赋值 clazz.getMethods() //类的方法(public修饰的方法,包含继承) clazz.getDeclaredMethods() //类的方法(类中定义的所有方法,不包含继承) method.getName() //方法名 method.getReturnType() //方法返回类型 method.getTypeParameters() //方法参数列表 clazz.getConstructors() //类的构造方法(public修饰的属性,不包含继承) clazz.getDeclaredConstructors() //类的构造方法(类中定义的所有属性,不包含继承) clazz.getPackage() //类所在的包
使用反射实例化
User user = new User("xxx","123");
User user = new User();
user.setUsername("xxx");
user.setPassword("123");
Class<User> clazz = User.class;
User user = clazz.newInstance();
Method setUsername = clazz.getMethod("setUsername",String.class);
setUsername.invoke(user,"xxx");
Method.setPassword = clazz.getMethod("setPassword",String.class);
setPassword.invoke(user,"123");
Class<User> clazz = User.class;
User user = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String methodName = "set"+field.getName().substring(0,1).toUpperCase()+name.substring(1);
Class<?> methodType = field.getType();
clazz.getMethod(methodName, methodType).invoke(user,"xxx");
}
Class<User> clazz = User.class;
User user = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
field.set(user,"xxx");
}
注解
含义:Annotation是从JDK1.5开始引入的新技术,注解即可以对程序员解释又可以对程序解释
注解 vs 注解
注释:对程序员解释代码信息
注解:对程序和程序员解释代码信息
内置注解
@Override:重写
@Deprecated:
@SuppressWarnings:all、unchecked、deprecation等
元注解
@Target:用于描述注解的适用范围
@Retention:注解的生命周期
@Document:该注解将包含在javadoc文档中
@Inherited:子类可继承父类该注解
自定义注解
Class<Teacher> clazz = Teacher.class;
Teacher tch = clazz.newInstance();
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
Value value = field.getAnnotation(Value.class);
if (value != null){
String v = value.value();
if ("".equals(v)) throw new RuntimeException();
field.setAccessible(true);
Class<?> type = field.getType();
if (type == String.class) field.set(tch,v);
else if (type.isPrimitive()){
if (type == int.class || type == Integer.class)
field.set(tch,Integer.parseInt(v));
else if (type == byte.class || type == Byte.class)
field.set(tch,Byte.parseByte(v));
else if (type == short.class || type == Short.class)
field.set(tch,Short.parseShort(v));
else if (type == long.class || type == Long.class)
field.set(tch,Long.parseLong(v));
else if (type == float.class || type == Float.class)
field.set(tch,Float.parseFloat(v));
else if (type == double.class || type == Double.class)
field.set(tch,Double.parseDouble(v));
else if (type == boolean.class || type == Boolean.class)
field.set(tch,Boolean.parseBoolean(v));
else if (type == char.class || type == Character.class)
field.set(tch,v.charAt(0));
}
}
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Value {
String value() default "";
}