在本教程中,我们将学习反射,这是 Java 编程中的一项功能,它使我们能够检查和修改类,方法等。
在 Java 中,反射使我们能够在运行时检查和操作类,接口,构造器,方法和字段。
Java Class类
在学习 Java 反射之前,我们需要了解一个名为Class的 Java 类。
Java 中有一个名为Class的类,可在运行时保留有关对象和类的所有信息。
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 classDog d1 = new Dog();// create an object of Class using getClass()Class obj = d1.getClass();// find the interfaces implemented by DogClass[] objInterface = obj.getInterfaces();for(Class c : objInterface) {// print the name of interfacesSystem.out.println("Interface Name: " + c.getName());}}catch(Exception e) {e.printStackTrace();}}}
输出
Interface Name: AnimalInterface 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 classDog d1 = new Dog();// create an object of Class using getClass()Class obj = d1.getClass();// Get the access modifier of Dog in integer formint modifier = obj.getModifiers();System.out.println("Modifier: " + Modifier.toString(modifier));// Find the superclass of DogClass superClass = obj.getSuperclass();System.out.println("Superclass: " + superClass.getName());}catch(Exception e) {e.printStackTrace();}}}
输出
Modifier: publicSuperclass: 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 ClassClass obj = d1.getClass();// manipulating the public field type of DogField field1 = obj.getField("type");// set the value of fieldfield1.set(d1, "labrador");// get the value of field by converting in StringString typeValue = (String)field1.get(d1);System.out.println("type: " + typeValue);// get the access modifier of typeint mod1 = field1.getModifiers();String modifier1 = Modifier.toString(mod1);System.out.println("Modifier: " + modifier1);System.out.println(" ");}catch(Exception e) {e.printStackTrace();}}}
输出:
type: labradorModifier: 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 ClassClass obj = d1.getClass();// accessing the private fieldField field2 = obj.getDeclaredField("color");// making the private field accessiblefield2.setAccessible(true);// set the value of colorfield2.set(d1, "brown");// get the value of type converting in StringString colorValue = (String)field2.get(d1);System.out.println("color: " + colorValue);// get the access modifier of colorint mod2 = field2.getModifiers();String modifier2 = Modifier.toString(mod2);System.out.println("modifier: " + modifier2);}catch(Exception e) {e.printStackTrace();}}}
输出:
color: brownmodifier: private
Java 方法的反射
像字段一样,我们可以使用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 ClassClass obj = d1.getClass();// get all the methods using the getDeclaredMethod()Method[] methods = obj.getDeclaredMethods();// get the name of methodsfor(Method m : methods) {System.out.println("Method Name: " + m.getName());// get the access modifier of methodsint modifier = m.getModifiers();System.out.println("Modifier: " + Modifier.toString(modifier));// get the return types of methodSystem.out.println("Return Types: " + m.getReturnType());System.out.println(" ");}}catch(Exception e) {e.printStackTrace();}}}
输出:
Method Name: displayModifier: publicReturn type: voidMethod Name: eatModifier: protectedReturn Type: voidMethod Name: makeSoundModifier: privateReturn 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 constructorsSystem.out.println("Constructor Name: " + c.getName());// get access modifier of constructorsint modifier = c.getModifiers();System.out.println("Modifier: " + Modifier.toString(modifier));// get the number of parameters in constructorsSystem.out.println("Parameters: " + c.getParameterCount());}}catch(Exception e) {e.printStackTrace();}}}
输出:
Constructor Name: DogModifier: publicParameters: 0Constructor Name: DogModifier: publicParameters: 1Constructor Name: DogModifier: privateParameters: 2
