1. Three reflection methods
There are three methods to obtain the Class object of a class: Class.forName(String className), className.class, instance object.getClass().
2. Method description
Reflection mechanism is implemented through new object
Reflection mechanism is implemented through path
Reflection is implemented through class name Mechanism
3, Example
public class Demo(){ F f=new F(); //第一种表达方式 Class c1=F.class;//这种表达方式同时也告诉了我们任何一个类都有一个隐含的静态成员变量class //第二种表达方式 Class c2=f.getClass();//这种表达方式在已知了该类的对象的情况下通过getClass方法获取 //第三种表达方式 Class c3 = null; try { c3 = Class.forName("com.text.F");//类的全称 } catch (ClassNotFoundException e) { e.printStackTrace(); } } class F{}
The above is the detailed content of What are the three methods of java reflection. For more information, please follow other related articles on the PHP Chinese website!