Acquaintance java reflection mechanism

There are so many in this small blog content - know how to use java reflection of the mentality of

What is the java reflection mechanism? When will use java reflection mechanism? How to use java reflection mechanism?


 

Then we started ~

(A) reflection (the Reflection)

JAVA reflection _ Baidu Encyclopedia (you do not intend to poke see it?)

java reflection mechanism to make this static Java language has become particularly prominent, dynamic java reflection mechanism that allows calling methods and properties of the class, you can call the object's method dynamically during operation.

* The degree of your mother interpreted in dynamic and static languages ​​these two words attention

So the difference between static and dynamic languages are - raindi - blog Park .

Static type of a variable can be determined depending on the context of the type of a variable, but it is to run dynamic languages ​​is determined by its definition at compile time

(B) Why reflection of it?

Java reflection mechanism allows to obtain the structure of a class at runtime, member variables, used to instantiate. Java's reflection mechanism it knows the basic structure of the class, the ability to ascertain the Java class structure, we called the Java class of "self-examination." This self-audit mechanism like when using eclipse - when the object .XXX compilation tools will pop up properties and methods of the corresponding object is self-audit mechanism. nice

(C) how to use java reflection mechanism?

1.Class class
what it is Class class?
In the java language, a class is an instance of java.lang.Class object (Class uppercase C), then you can say I create an object such as object class Class student is, "students" category itself.

2. Reflector

Now I want to use java reflection mechanism to access the "students" category so I can declare

  public class Domo {
     public void f() {
    	//声明学生对象1
    	Student ni = new Student();
    	//声明学生对象2
    	Class a = Student.class;//也就是每一个对象都有一个class属性
    	//声明学生对象3
    	Class b = ni.getClass();//通过已知对象来获得对应的类;
    	//声明学生对象4
    	try {
    		Class c3 = Class.forName("one.Student");//找到类的路径
    		} catch (ClassNotFoundException e) {
    		e.printStackTrace();
    		}
    }
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}

}

Here are abc classes have expressed Student class type, then abc is equal it?

Practice output "a == b" return value is true only visible to a class object is an instance of Class.

----- 1 via create an instance of an object

通过a,b,c的newInstance来创
try {
			Student a1 =(Student)a.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 		

-----2调用对象的方法

通过调用对象的方法,方法其实也是实例化对象,它是Method的实例化对象。

	Method[] ms = c.getMethods();//获得类里面的方法;
    			for(int i = 0; i < ms.length;i++){
    				//然后可以得到方法的名称
    				System.out.print(ms[i].getName()+"   ");
    			}

获得对象的参数,和获得方法一样,属性也有它的类-》Field

Field[] fs = c.getDeclaredFields();//可以这样获得方法,这里的方法不管是私有还是共有都可以获取

-----3对象的构造函数

同理构造函数也有它的类-》Constructor

Constructor[] cs = c.getDeclaredConstructor;//获取它的全部构造函数


谈谈Java反射机制~我有认真看他的博客

Java基础之-反射(非常重要) - CSDN博客~

Guess you like

Origin www.cnblogs.com/ttnrt/p/11086554.html