Java中的反射

反射是一种API,用于在运行时检查或修改方法、类和接口的行为。

null
  • 反射所需的类在java下提供。朗。反映包裹。
  • 反射为我们提供了有关对象所属的类的信息,以及可以使用该对象执行的该类的方法。
  • 通过反射,我们可以在运行时调用方法,而无需考虑与它们一起使用的访问说明符。

reflection 反射可用于获取以下信息——

  1. getClass()方法用于获取对象所属类的名称。
  2. 建设者 getConstructors()方法用于获取对象所属类的公共构造函数。
  3. 方法 getMethods()方法用于获取对象所属类的公共方法。

// A simple Java program to demonstrate the use of reflection
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.lang.reflect.Constructor;
// class whose object is to be created
class Test
{
// creating a private field
private String s;
// creating a public constructor
public Test()  {  s = "GeeksforGeeks" ; }
// Creating a public method with no arguments
public void method1()  {
System.out.println( "The string is " + s);
}
// Creating a public method with int as argument
public void method2( int n)  {
System.out.println( "The number is " + n);
}
// creating a private method
private void method3() {
System.out.println( "Private method invoked" );
}
}
class Demo
{
public static void main(String args[]) throws Exception
{
// Creating object whose property is to be checked
Test obj = new Test();
// Creating class object from the object using
// getclass method
Class cls = obj.getClass();
System.out.println( "The name of class is " +
cls.getName());
// Getting the constructor of the class through the
// object of the class
Constructor constructor = cls.getConstructor();
System.out.println( "The name of constructor is " +
constructor.getName());
System.out.println( "The public methods of class are : " );
// Getting methods of the class through the object
// of the class by using getMethods
Method[] methods = cls.getMethods();
// Printing method names
for (Method method:methods)
System.out.println(method.getName());
// creates object of desired method by providing the
// method name and parameter class as arguments to
// the getDeclaredMethod
Method methodcall1 = cls.getDeclaredMethod( "method2" ,
int . class );
// invokes the method at runtime
methodcall1.invoke(obj, 19 );
// creates object of the desired field by providing
// the name of field as argument to the
// getDeclaredField method
Field field = cls.getDeclaredField( "s" );
// allows the object to access the field irrespective
// of the access specifier used with the field
field.setAccessible( true );
// takes object and the new value to be assigned
// to the field as arguments
field.set(obj, "JAVA" );
// Creates object of desired method by providing the
// method name as argument to the getDeclaredMethod
Method methodcall2 = cls.getDeclaredMethod( "method1" );
// invokes the method at runtime
methodcall2.invoke(obj);
// Creates object of the desired method by providing
// the name of method as argument to the
// getDeclaredMethod method
Method methodcall3 = cls.getDeclaredMethod( "method3" );
// allows the object to access the method irrespective
// of the access specifier used with the method
methodcall3.setAccessible( true );
// invokes the method at runtime
methodcall3.invoke(obj);
}
}


输出:

The name of class is Test
The name of constructor is Test
The public methods of class are : 
method2
method1
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
The number is 19
The string is JAVA
Private method invoked

重要观察结果:

  1. 如果我们知道一个方法的名称和参数类型,就可以通过反射调用它。为此,我们使用以下两种方法 getDeclaredMethod(): 创建要调用的方法的对象。这个方法的语法是
    Class.getDeclaredMethod(name, parametertype)
    name- the name of method whose object is to be created
    parametertype - parameter is an array of Class objects

    调用(): 为了在运行时调用类的方法,我们使用以下方法——

    Method.invoke(Object, parameter)
    If the method of the class doesn’t accepts any 
    parameter then null is passed as argument.
  2. 通过反思,我们可以 访问私有变量和方法 在类对象的帮助下调用一个类,并使用上面讨论的对象调用该方法。为此,我们使用以下两种方法。

    班getDeclaredField(字段名): 过去是私人场地。返回指定字段名的字段类型的对象。 领域setAccessible(true): 允许访问字段,而不考虑与字段一起使用的访问修饰符。

    使用反射的优点:

    • 扩展功能: 应用程序可以通过使用扩展性对象的完全限定名创建扩展性对象的实例来使用外部的、用户定义的类。
    • 调试和测试工具 :调试器使用反射属性检查类上的私有成员。

    缺点:

    • 性能开销: 反射操作的性能比非反射操作慢,在性能敏感的应用程序中经常调用的代码部分应该避免使用反射操作。
    • 内部构件的暴露: 反射代码打破了抽象,因此可能会随着平台的升级而改变行为。

    参考: https://docs.oracle.com/javase/tutorial/reflect/index.html

    本文由 阿卡什奥哈 .如果你喜欢GeekSforgek,并且想贡献自己的力量,你也可以写一篇文章,并将文章邮寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

    如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享