二、JAVA反射机制--进阶篇详解

目录

 

一、Constructors

二、Methods

三、Getter and Setter

四、 Private fields and methods

六、 Dynamic Class Loading and Reloading


一、Constructors

包括构造方法的获得及实例化对象。

package design;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.Annotation;
import java.text.DateFormat.Field;
import java.util.Scanner;
import ch13.Student;
//import java.lang.reflect.Field;
public abstract class Main {
	public static  void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
		String info="ch13.Student";
		Class class1=Class.forName(info);
		System.out.println(class1);
		//获取类名
		String ClassName=class1.getName();
		Sop(ClassName);
		//获取不带包名的类名
		ClassName=class1.getSimpleName();
		Sop(ClassName);
		//获取构造函数
		Constructor []constructors=class1.getConstructors();
		for(Constructor c1:constructors)
		{
			Sop(c1);
		}
		//获取特定参数构造函数
		Constructor constructor=class1.getConstructor(new Class[]{String.class,String.class,int.class});
		Sop(constructor);
		Sop("----");
		//获取构造函数参数类型
		Class []parameterTypes =constructor.getParameterTypes();
		for(Class c2:parameterTypes)
		{
			Sop(c2);
		}
		for(Constructor c1:constructors)
		{
			Class []parameterType1 =c1.getParameterTypes();
			for(Class c2:parameterTypes)
			{
				Sop(c2);
			}
		}
		Sop("----");
		//实例化一个对象并调用相关函数
		Student stu=(Student)constructor.newInstance("20171789","pre",19);
		stu.showInformation();
		
		
	}

	private static void Sop(Object className) {
		// TODO Auto-generated method stub
		System.out.println(className);
	}

}

二、Methods

包括一些对象实例化以及方法的调用。

package design;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.Annotation;
import java.text.DateFormat.Field;
import java.util.Scanner;
import ch13.Student;
//import java.lang.reflect.Field;
public abstract class Main {
	public static  void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
		String info="ch13.Student";
		Class class1=Class.forName(info);
		System.out.println(class1);
		//获取类名
		String ClassName=class1.getName();
		Sop(ClassName);
		//获取不带包名的类名
		ClassName=class1.getSimpleName();
		Sop(ClassName);
		//获取指定类型的
		Sop("---");
		Method m1=class1.getDeclaredMethod("setName", String.class);
		Sop(m1.getName());
		Sop(m1);
		m1=class1.getDeclaredMethod("setName1", String.class);
		Sop(m1);
		m1=class1.getDeclaredMethod("showInformation", null);
		Sop(m1);
		//一般getDeclared...可以得到任意类型的方法、成员
		Sop("---");
		//获取函数参数类型
		Method m2s[]=class1.getDeclaredMethods();
		for(Method m2:m2s)
		{
			Class []para=m2.getParameterTypes();
			for(Class m3:para)
			{
				Sop(m3);
			}
		}
		//获取函数返回值类型
		for(Method m2:m2s)
		{
			Class m4=m2.getReturnType();
			Sop(m4.getName());
		}
		//
		Constructor c1=class1.getConstructor();
		Object obj=c1.newInstance();
		Student stu=(Student)obj;
		Sop("---");
		m1=class1.getMethod("setId", String.class);
		stu.showInformation();
		m1.invoke(obj, "2017");//调用函数---
		stu.showInformation();
		m1=class1.getMethod("setGrade", int.class);
		m1.invoke(obj, 100);
		stu.showInformation();
		//这样就完成了各项信息的变化
		
		
		
		
		
		
		
		
		
		/*//获取构造函数
		Constructor []constructors=class1.getConstructors();
		for(Constructor c1:constructors)
		{
			Sop(c1);
		}
		//获取特定参数构造函数
		Constructor constructor=class1.getConstructor(new Class[]{String.class,String.class,int.class});
		Sop(constructor);
		Sop("----");
		//获取构造函数参数类型
		Class []parameterTypes =constructor.getParameterTypes();
		for(Class c2:parameterTypes)
		{
			Sop(c2);
		}
		for(Constructor c1:constructors)
		{
			Class []parameterType1 =c1.getParameterTypes();
			for(Class c2:parameterTypes)
			{
				Sop(c2);
			}
		}
		Sop("----");
		//实例化一个对象并调用相关函数
		Student stu=(Student)constructor.newInstance("20171789","pre",19);
		stu.showInformation();
		
		*/
	}

	private static void Sop(Object className) {
		// TODO Auto-generated method stub
		System.out.println(className);
	}

}

三、Getter and Setter

package design;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.Annotation;
import java.text.DateFormat.Field;
import java.util.Scanner;
import ch13.Student;

//import java.lang.reflect.Field;
public abstract class Main {
	public static  void main(String[] args)
			throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, InstantiationException {
		String info = "ch13.Student";
		Class class1 = Class.forName(info);
		System.out.println(class1);
		// 获取类名
		String ClassName = class1.getName();
		Sop(ClassName);
		// 获取不带包名的类名
		ClassName = class1.getSimpleName();
		Sop(ClassName);
		Sop("下面是获取特定函数");
		Method[] ms = class1.getMethods();
		for (Method m : ms) {
			if (isGetter(m)) {
				Sop("isGetter:" + m);
			} else if (isSetter(m)) {
				Sop("isSetter:" + m);
			}
		}
		Sop("---");
		

	}

	private static boolean isSetter(Method m) {
		// TODO Auto-generated method stub
		if (!m.getName().startsWith("get"))
			return false;
		else if (m.getParameterTypes().length != 0)
			return false;
		else if (void.class.equals(m.getReturnType()))
			return false;
		return true;

	}

	private static boolean isGetter(Method m) {
		// TODO Auto-generated method stub
		if (!m.getName().startsWith("set"))
			return false;
		else if (m.getParameterTypes().length != 1)
			return false;
		return true;
	}

	private static void Sop(Object className) {
		// TODO Auto-generated method stub
		System.out.println(className);
	}

}

四、 Private fields and methods

在第一遍文章中已经演示过,这里只给出一个小荔枝。

package design;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.Annotation;
import java.text.DateFormat.Field;
import java.util.Scanner;
import ch13.Student;

//import java.lang.reflect.Field;
public abstract class Main {
	public static  void main(String[] args)
			throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, InstantiationException {
		String info = "ch13.Student";
		Class class1 = Class.forName(info);
		System.out.println(class1);
		// 获取类名
		String ClassName = class1.getName();
		Sop(ClassName);
		// 获取私有函数
		Method privateMethod=class1.getDeclaredMethod("setName1", String.class);
		privateMethod.setAccessible(true);
		Object obj=class1.newInstance();
		Student stu=(Student)obj;
		String value=(String) privateMethod.invoke(stu, "haha");
		Sop(value);
		stu.showInformation();
		/*
		///
		
		ClassName = class1.getSimpleName();
		Sop(ClassName);
		Sop("下面是获取特定函数");
		Method[] ms = class1.getMethods();
		for (Method m : ms) {
			if (isGetter(m)) {
				Sop("isGetter:" + m);
			} else if (isSetter(m)) {
				Sop("isSetter:" + m);
			}
		}
		Sop("---");*/
		

	}

	private static boolean isSetter(Method m) {
		// TODO Auto-generated method stub
		if (!m.getName().startsWith("get"))
			return false;
		else if (m.getParameterTypes().length != 0)
			return false;
		else if (void.class.equals(m.getReturnType()))
			return false;
		return true;

	}

	private static boolean isGetter(Method m) {
		// TODO Auto-generated method stub
		if (!m.getName().startsWith("set"))
			return false;
		else if (m.getParameterTypes().length != 1)
			return false;
		return true;
	}

	private static void Sop(Object className) {
		// TODO Auto-generated method stub
		System.out.println(className);
	}

}

五、Generics

六、 Dynamic Class Loading and Reloading

 

 

你可能感兴趣的:(设计模式,设计模式)