方法一:直接通过一个
class
的静态变量class
获取:Class cls = String.class;
方法二:如果我们有一个实例变量,可以通过该实例变量提供的
getClass()
方法获取:String s = "Hello";
Class cls = s.getClass();
方法三:如果知道一个
class
的完整类名,可以通过静态方法Class.forName()
获取:Class cls = Class.forName("java.lang.String");
访问字段
```java public class Reflection02 { public static void Run() throws NoSuchFieldException {
Class<Student> stdClass = Student.class;
//获取public字段"score":
System.out.println(stdClass.getField("score"));
//获取继承的public字段"name"
System.out.println(stdClass.getField("name"));
//获取继承的private字段"grade"
System.out.println(stdClass.getDeclaredField("grade"));
} }
class Student extends Person { public int score; private int grade; }
class Person { public String name; }
<a name="HbEsk"></a>
### 修改字段
```java
public class Main {
public static void main(String[] args) throws Exception {
Person p = new Person("Xiao Ming");
System.out.println(p.getName()); // "Xiao Ming"
Class c = p.getClass();
Field f = c.getDeclaredField("name");
f.setAccessible(true);
f.set(p, "Xiao Hong");
System.out.println(p.getName()); // "Xiao Hong"
}
}
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
调用方法
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws NoSuchMethodException {
Class<Student> stdClass = Student.class;
//获取public方法的getScore,参数为String
Method getScore = stdClass.getMethod("getScore", String.class);
System.out.println(getScore);
//获取继承的public方法getName 无参数
System.out.println(stdClass.getMethod("getName"));
//获取继承的private方法getGrade参数为int
System.out.println(stdClass.getDeclaredMethod("getGrade", int.class));
}
}
class Student extends Person {
public int getScore(String type) {
return 99;
}
private int getGrade(int year) {
return 1;
}
}
class Person {
public String getName() {
return "Person";
}
}
public class Main {
public static void main(String[] args) throws Exception {
// String对象:
String s = "Hello world";
// 获取String substring(int)方法,参数为int:
Method m = String.class.getMethod("substring", int.class);
// 在s对象上调用该方法并获取结果:
String r = (String) m.invoke(s, 6);
// 打印调用结果:
System.out.println(r);
}
}
/*
调用静态方法
*/
public class Main {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method m = Integer.class.getMethod("parseInt", String.class);
Integer n = (Integer) m.invoke(null, "12345");
System.out.println(n);
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/*
调用非public方法
*/
public class Main {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Person p = new Person();
Method m = p.getClass().getDeclaredMethod("setName", String.class);
m.setAccessible(true);
m.invoke(p, "Bob");
System.out.println(p.name);
}
}
class Person {
String name;
private void setName(String name) {
this.name = name;
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/*
调用构造方法
*/
public class Main {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//获取构造方法
Constructor<Integer> cons1 = Integer.class.getConstructor(int.class);
//调用构造方法
Integer n1 = cons1.newInstance(123);
System.out.println(n1);
//获取构造方法
Constructor<Integer> cons2 = Integer.class.getConstructor(String.class);
Integer n2 = cons2.newInstance("456");
System.out.println(n2);
}
}
/*
获取父类的class
*/
public class Main {
public static void main(String[] args) {
Class<Integer> i = Integer.class;
Class<? super Integer> n = i.getSuperclass();
System.out.println(n);
Class<? super Integer> o = n.getSuperclass();
System.out.println(o);
System.out.println(o.getSuperclass());
}
}
/*
获取interface接口
*/
public class Main {
public static void main(String[] args) {
Class<Integer> integerClass = Integer.class;
Class<?>[] is = integerClass.getInterfaces();
for (Class<?> i : is) {
System.out.println(i);
}
}
}
动态代理
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Main {
public static void main(String[] args) {
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(method);
if (method.getName().equals("morning")) {
System.out.println("Good morning, " + args[0]);
}
return null;
}
};
Hello hello = (Hello) Proxy.newProxyInstance(
Hello.class.getClassLoader(), // 传入ClassLoader
new Class[]{Hello.class}, // 传入要实现的接口
handler); // 传入处理调用方法的InvocationHandler
hello.morning("Bob");
}
}
interface Hello {
void morning(String name);
}