是在运行时动态访问类和对象的技术
在JDK1.2版本之后的高级特性,隶属于java.lang.reflect
核心类
Class类
- Class类是JVM中代表“类和接口”的类
- Class对象具体包含了某个特定类的结构信息
- 通过Class可以获取对应类的构造方法/方法/成员变量
方法 | 用途 |
---|---|
Class.forName() | 静态方法,用于获取指定Class对象 |
classObj.newInstance() | 通过默认构造方法创建新的对象 |
classObj.getConstructor() | 获取指定的public修饰构造方法Constructor对象 |
classObj.getMethod() | 获取指定的public修饰方法Method对象 |
classObj.getField() | 获取指定的public修饰成员变量Field对象 |
public class ClassSample {
public static void main(String[] args) {
try {
Class employee = Class.forName("shop.dragon.reflect.Employee");
Employee emp = (Employee)employee.newInstance();
System.out.println(emp);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Method类核心方法
方法 | 用途 |
---|---|
classObj.getMethod() | 获取指定public修饰的方法对象 |
methodObj.invoke() | 调用指定对象的对应方法 |
public class MethSample {
public static void main(String[] args) {
try {
Class employeeClass = Class.forName("shop.dragon.reflect.Employee");
Constructor constructor = employeeClass.getConstructor(new Class[]{
Integer.class,String.class,Float.class,String.class
});
Employee emp = (Employee)constructor.newInstance(new Object[]{
100,"李磊",3000f,"研发部"
});
Method updateSalary = employeeClass.getMethod("updateSalary", Float.class);
Employee employee1 = (Employee)updateSalary.invoke(emp, new Object[]{
1000f
});
System.out.println(employee1);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Field成员变量类
方法 | 用途 |
---|---|
classObj.getField() | 获取指定的public修饰的成员变量对象 |
fieldObj.set() | 为某个对象指定成员变量赋值 |
fieldObj.get() | 获取某个对象指定成员变量数值 |
public class FieldSample {
public static void main(String[] args) {
try {
Class employeeClass = Class.forName("shop.dragon.reflect.Employee");
Constructor constructor = employeeClass.getConstructor(new Class[]{
Integer.class, String.class, Float.class, String.class
});
Employee emp1 = (Employee) constructor.newInstance(new Object[]{
100, "李明", 1000f, "研发部"
});
Field enameField = employeeClass.getField("ename");
String ename = (String) enameField.get(emp1);
System.out.println(ename);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
getDeclared系列方法
getDeclaredConstructor(s)|Method(s)|Field(s)
相比于前面的可以获取private修饰的方法或变量。
public class getDeclaredSample {
public static void main(String[] args) {
try {
Class employeeClass = Class.forName("shop.dragon.reflect.Employee");
Constructor constructor = employeeClass.getConstructor(new Class[]{
Integer.class, String.class, Float.class, String.class
});
Employee emp1 = (Employee) constructor.newInstance(new Object[]{
100, "黎明", 1002f, "销售部"
});
Field[] declaredFields = employeeClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
if (declaredField.getModifiers() == 1) {
/**
* public修饰
*/
Object val = declaredField.get(emp1);
System.out.println(declaredField.getName() + ":" + val);
} else if (declaredField.getModifiers() == 2) {
/**
* private修饰
*/
Method method = employeeClass.getMethod("get" + declaredField.getName().substring(0, 1).toUpperCase()
+ declaredField.getName().substring(1));
Object ret = method.invoke(emp1);
System.out.println(declaredField.getName() + ":" + ret);
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
获取properties文件
config.properties文件
language = zh-cn
Application
为类名
Properties properties = new Properties();
String configPath = Applictation.class.getResource("/config.properties");
configPath = new URLDecoder().decode(configPath, "UTF-8");
properties.load(new FileInputStream(configPath));
String language = properties.getProperty("language");
...