Operation reflection

Reflection is a new object is created, properties and methods of operating techniques.

A total of three types: operating construction methods, operating methods and properties; only method discussed in this blog.

Acquisition method used in class:

public class Student {

	public String name;
	
	public void doHomework() {
		System.out.println(name+"正在做作业。。。。。。");
	}
	
	public void playing() {
		System.out.println(name+"正在玩耍。。。。。。");
	}
}

Get Method: Note: Corresponding Method class class class

package venus;

import java.lang.reflect.Method;

public class ConstructionTest {

	public static void main(String[] args) {
		
		
		Class clazz = Student.class;
		
		//获取方法
		try {
			//获取访问权限为public的方法 包含继承的父类的方法
			Method method = clazz.getMethod("doHomework"); 
			System.out.println(method);
			
			//获取继承于父类的 toString 方法
			method = clazz.getMethod("toString");  
			System.out.println(method);
			
			//不区分访问权限获取方法 不包含继承的父类的方法
			method = clazz.getDeclaredMethod("playing");  
			System.out.println(method);
			
			//获取访问权限为public的所有方法 包含继承的父类的方法
			Method [] methods = clazz.getMethods();
			System.out.println(methods.length);
			
			//不区分访问权限获取所有的方法 不包含继承的父类的方法
			methods = clazz.getDeclaredMethods();  
			System.out.println(methods.length);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

note:

  • Method [] getMethods () : returns the Class object representing the class or interface for all public methods ( including inherited ) corresponding Method object array.
  • Method getMethod (String methodName , Class ... <?> The parameterTypes ) : Returns the Class represent classes or interfaces method name and method that matches the type parameter of the object public methods ( including inherited ) in Method object.
  • Method [] getDeclaredMethods () : This returns a Class object represents a class or method (all declared within an interface defined access rights contain inherited ) corresponding Method object array.
  • Method getDeclaredMethod (String methodName, Class ... <?> The parameterTypes ) : Returns the Class represent classes or interfaces method name and method parameter matches the type of process (the object excluding inheritance ) corresponding Method object.

Displayed information is as follows:

Check method used in class;

public class Student {

	public String name;
	
	private void doHomework(String name) {
		System.out.println(name+"正在做作业。。。。。。");
	}
}

Viewing Method information:

package venus;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ConstructionTest {

	public static void main(String[] args) {
		
		
		Class clazz = Student.class;
		
		//获取方法详细信息
		try {
			Method method = clazz.getDeclaredMethod("doHomework", String.class);
			
			//获取声明Method对象对应方法的类的class对象
			Object obj = method.getDeclaringClass();
			System.out.println(obj);
			
			//以整数形式返回Method对象表示的方法的修饰符
			int mod = method.getModifiers();
			String result = Modifier.toString(mod);
			System.out.println(result);
			
			//获取Method队形的返回值类型
			clazz = method.getReturnType();
			System.out.println(clazz);
			
			//获取方法名
			result = method.getName();
			System.out.println(result);
			
			//获取方法的参数列表
			Class [] types =method.getParameterTypes();
			System.out.println(types.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

note:

  • Class <?> The getDeclaringClass () Returns statement: Method, an object representation of the class or interface Class objects.
  • int getModifiers () : Returns an integer Method object modifier method represented. Should use the Modifier class toString integer returned decoded (see Example Remarks) method.
  • Class <?> GetReturnType () : Returns the Method object represents the return type value corresponding to Class objects.
  • String getName () : Returns the name.
  • Class [] <?> GetParameterTypes () : returns the Method the corresponding parameter representative of the type of object methods Class array of objects. If the method has no parameters, the array length is 0 .
  • Class [] <?> GetExceptionTypes () : Returns a Method object that represents a method corresponding to the type of exception thrown Class array of objects. If this method is not in its throws exception return clause declared length 0 of the array.

Displayed information is as follows:

Call the method:

package venus;

import java.lang.reflect.Method;

public class ConstructionTest {

	public static void main(String[] args) {

		Class clazz = Student.class;
		
		//方法调用
		try {	
			Method method = clazz.getDeclaredMethod("doHomework", String.class);
			method.setAccessible(true);
			Object obj = method.invoke(new Student(), "Tom");  //反射不区分方法是否为静态方法
			System.out.println(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

note:

  • void setAccessible ( boolean Flag) : whether to ignore the impact of access when calling methods, to true omit, false representation not be ignored.
  • Invoke Object (Object obj, ... Object args ) : calling Method The method refers to an object and return Object type result. obj represents class instances where this method, if the static method when the obj may be specified as null; args represents arguments passed to the method, if the method has no parameters, the args array length may be 0 or null .

Displayed information is as follows:

Published 99 original articles · won praise 3 · Views 1229

Guess you like

Origin blog.csdn.net/qq_44971038/article/details/103878891