在 Java 中,反射使我们能够在运行时检查和操作类,接口,构造器,方法和字段。
在学习 Java 反射之前,我们需要了解一个名为Class的 Java 类。
Java 中有一个名为Class的类,可在运行时保留有关对象和类的所有信息。
Class的对象描述特定类的属性。 该对象用于执行反射。
我们可以通过创建Class的对象
- 使用
forName()方法
forName()接受字符串参数(类的名称)并返回Class的对象。 返回的对象引用由字符串指定的类。 例如,
Class Dog { }
Class c1 = Class.forName("Dog"); - 使用
getClass()方法
getClass()方法使用特定类的对象创建Class的新对象。 例如,
Dog d1 = new Dog()
Class c1 = d1.getClass(); - 使用
.class
我们还可以使用.class扩展名创建Class的对象。 例如,
Class c1 = Dog.class; 创建Class的对象后,我们可以使用这些对象进行反射。
我们可以使用Class的getInterfaces()方法来收集有关由该类实现的接口的信息。 此方法返回接口数组。
import java.lang.Class;
import java.lang.reflect.*;
interface Animal {
public void display();
}
interface Mammal {
public void makeSound();
}
class Dog implements Animal, Mammal {
public void display() {
System.out.println("I am a dog.");
}
public void makeSound() {
System.out.println("Bark bark");
}
}
class ReflectionDemo {
public static void main(String[] args) {
try {
// create an object of Dog class
Dog d1 = new Dog();
// create an object of Class using getClass()
Class obj = d1.getClass();
// find the interfaces implemented by Dog
Class[] objInterface = obj.getInterfaces();
for(Class c : objInterface) {
// print the name of interfaces
System.out.println("Interface Name: " + c.getName());
}
}
catch(Exception e) {
e.printStackTrace();
}
}
} 输出
Interface Name: Animal
Interface Name: Mammal 类Class的方法getSuperclass()可用于获取有关特定类超类的信息。
而且,Class提供了一种方法getModifier(),该方法以整数形式返回类的修饰符。
import java.lang.Class;
import java.lang.reflect.*;
interface Animal {
public void display();
}
public class Dog implements Animal {
public void display() {
System.out.println("I am a dog.");
}
}
class ReflectionDemo {
public static void main(String[] args) {
try {
// create an object of Dog class
Dog d1 = new Dog();
// create an object of Class using getClass()
Class obj = d1.getClass();
// Get the access modifier of Dog in integer form
int modifier = obj.getModifiers();
System.out.println("Modifier: " + Modifier.toString(modifier));
// Find the superclass of Dog
Class superClass = obj.getSuperclass();
System.out.println("Superclass: " + superClass.getName());
}
catch(Exception e) {
e.printStackTrace();
}
}
} 输出
Modifier: public
Superclass: Animal 要了解Class的更多方法,请访问 java.lang.Class 。
包java.lang.reflect提供了可用于操纵类成员的类。 例如,
Method类 - 提供有关类中方法的信息Field类 - 提供有关类中字段的信息Constructor类 - 提供有关类中构造器的信息
我们可以使用Field类提供的各种方法来检查和修改类的不同字段。
getFields()- 返回该类及其超类的所有公共字段getDeclaredFields()- 返回类的所有字段getModifier()- 以整数形式返回字段的修饰符set(classObject, value)- 使用指定的值设置字段的值get(classObject)- 获取字段的值setAccessible(boolean)- 使私有字段可访问
注意:如果我们知道字段名称,则可以使用
getField("fieldName")- 从类返回名称为fieldName的公共字段。getDeclaredField("fieldName")- 从类返回名称为fieldName的字段。
要了解Field类的更多方法,请访问字段类。
import java.lang.Class;
import java.lang.reflect.*;
class Dog {
public String type;
}
class ReflectionDemo {
public static void main(String[] args) {
try{
Dog d1 = new Dog();
// create an object of the class Class
Class obj = d1.getClass();
// manipulating the public field type of Dog
Field field1 = obj.getField("type");
// set the value of field
field1.set(d1, "labrador");
// get the value of field by converting in String
String typeValue = (String)field1.get(d1);
System.out.println("type: " + typeValue);
// get the access modifier of type
int mod1 = field1.getModifiers();
String modifier1 = Modifier.toString(mod1);
System.out.println("Modifier: " + modifier1);
System.out.println(" ");
}
catch(Exception e) {
e.printStackTrace();
}
}
} 输出:
type: labrador
Modifier: public import java.lang.Class;
import java.lang.reflect.*;
class Dog {
private String color;
}
class ReflectionDemo {
public static void main(String[] args) {
try {
Dog d1 = new Dog();
// create an object of the class Class
Class obj = d1.getClass();
// accessing the private field
Field field2 = obj.getDeclaredField("color");
// making the private field accessible
field2.setAccessible(true);
// set the value of color
field2.set(d1, "brown");
// get the value of type converting in String
String colorValue = (String)field2.get(d1);
System.out.println("color: " + colorValue);
// get the access modifier of color
int mod2 = field2.getModifiers();
String modifier2 = Modifier.toString(mod2);
System.out.println("modifier: " + modifier2);
}
catch(Exception e) {
e.printStackTrace();
}
}
} 输出:
color: brown
modifier: private 像字段一样,我们可以使用Method类提供的各种方法来检查类的不同方法。
getMethods()- 返回该类及其超类的所有公共方法getDeclaredMethod()- 返回该类的所有方法getName()- 返回方法的名称getModifiers()- 以整数形式返回方法的访问修饰符getReturnType()- 返回方法的返回类型
要了解有关Method类的更多方法的信息,请访问方法类。
import java.lang.Class;
import java.lang.reflect.*;
class Dog {
public void display() {
System.out.println("I am a dog.");
}
protected void eat() {
System.out.println("I eat dog food.");
}
private void makeSound() {
System.out.println("Bark Bark");
}
}
class ReflectionDemo {
public static void main(String[] args) {
try {
Dog d1 = new Dog();
// create an object of Class
Class obj = d1.getClass();
// get all the methods using the getDeclaredMethod()
Method[] methods = obj.getDeclaredMethods();
// get the name of methods
for(Method m : methods) {
System.out.println("Method Name: " + m.getName());
// get the access modifier of methods
int modifier = m.getModifiers();
System.out.println("Modifier: " + Modifier.toString(modifier));
// get the return types of method
System.out.println("Return Types: " + m.getReturnType());
System.out.println(" ");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
} 输出:
Method Name: display
Modifier: public
Return type: void
Method Name: eat
Modifier: protected
Return Type: void
Method Name: makeSound
Modifier: private
Return Type: void 我们还可以使用Constructor类提供的各种方法来检查类的不同构造器。
getConstructors()- 返回该类的所有公共构造器以及该类的超类getDeclaredConstructor()- 返回所有构造器getName()- 返回构造器的名称getModifiers()- 以整数形式返回构造器的访问修饰符getParameterCount()- 返回构造器的参数数量
要了解Constructor类的更多方法,请访问构造器类
import java.lang.Class;
import java.lang.reflect.*;
class Dog {
public Dog() {
}
public Dog(int age) {
}
private Dog(String sound, String type) {
}
}
class ReflectionDemo {
public static void main(String[] args) {
try {
Dog d1 = new Dog();
Class obj = d1.getClass();
// get all the constructors in a class using getDeclaredConstructor()
Constructor[] constructors = obj.getDeclaredConstructors();
for(Constructor c : constructors) {
// get names of constructors
System.out.println("Constructor Name: " + c.getName());
// get access modifier of constructors
int modifier = c.getModifiers();
System.out.println("Modifier: " + Modifier.toString(modifier));
// get the number of parameters in constructors
System.out.println("Parameters: " + c.getParameterCount());
}
}
catch(Exception e) {
e.printStackTrace();
}
}
} 输出:
Constructor Name: Dog
Modifier: public
Parameters: 0
Constructor Name: Dog
Modifier: public
Parameters: 1
Constructor Name: Dog
Modifier: private
Parameters: 2