Java反射机制学习(一)

首先先说以下反射使用的基本步骤(套路):

 

基本步骤:
1.获取目标类型相应的Class对象
2.调用Class对象内省方法获取目标类成员信息
3.访问目标类成员信息/操作目标类中的成员

 

//通过反射获取该类的field,method,constructor,interface,package and superClass informations
public class Car {
	private String brand;
	private int price;
	private static int count = 10;
	
	public Car(){
		
	}
	
	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public static int getCount() {
		return count;
	}

	public static void setCount(int count) {
		Car.count = count;
	}

	public Car(String brand, int price)
	{
		this.brand = brand;
		this.price = price;
	}
	
	public String toString(){
		return "The brand of this car is "+ brand +". The price is "+ price +"The number of this car is " + count;
	}
}

  通过下面这个类,利用反生机制,来一步一步的获取类Car的信息,

 field 信息    method信息 superclass 信息 interface信息 以及 package等等

 使用的方法其实都很容易看懂,也很容易实现。

 

public class Reflection1 {

	public static void parseFiled(String className) throws ClassNotFoundException{
		Class c = Class.forName(className);
		
		//parse the field 
		Field[] fields = c.getDeclaredFields();
		for(Field field: fields ){
			System.out.println("***********************************************");
			System.out.println("\t field info    :  " + field.toString());
			System.out.println("\t data type     :  " + field.getType());
			System.out.println("\t field name    :  " + field.getName());
			System.out.println("\t modifier      :  " + Modifier.toString(field.getModifiers()));				
		}
	} 
	public static void parseMethod(String className) throws ClassNotFoundException{
		Class c = Class.forName(className); 
		Method[] methods = c.getDeclaredMethods();
		for(Method method:methods){
			System.out.println("***********************************************");
			System.out.println("\t method        :  " + method.toString());
			System.out.println("\t method name   :  " + method.getName());
			System.out.println("\t modifier      :  " + Modifier.toString(method.getModifiers()));
			Class parameters[] = method.getParameterTypes();
			System.out.print("\t parameters    :  ");
			for(int i =0; i < parameters.length; i++){
				Class clazz = parameters[i];
				if(i!=0) 
				{
					System.out.print(",");
				}
				System.out.print(clazz);
			}
			System.out.println();
			System.out.println("\t return values :  " + method.getReturnType());

		}
	}
	public static void parseConstructor(String className) throws ClassNotFoundException{
		 Class c = Class.forName(className);
		 Constructor[] constructors = c.getDeclaredConstructors();
		 for(Constructor constructor: constructors)
		 {
			System.out.println("***********************************************");
			System.out.println("\t constructor         :  " + constructor.toString());
			System.out.println("\t constructor name    :  " + constructor.getName());
			System.out.println("\t constructor modifier:  " + Modifier.toString(constructor.getModifiers()));
			System.out.print("\t paramenters         :  ");
			Class parameters[] = constructor.getParameterTypes();
			for(int i=0; i< parameters.length;i++)
			{
				Class clazz = parameters[i];
				if(i!=0)
				{
					System.out.print(",");
				}
				System.out.print(clazz);
			}
			System.out.println();
		 }
	}
	public static void getTheSuperClassInfo(String className) throws ClassNotFoundException{
		 Class c = Class.forName(className); 
		 Class superClass = c.getSuperclass();
		 System.out.println("***********************************************");
		 System.out.println( className + "'s super class is :  " + superClass.toString());
	}
	public static void getTheInterfaceInfo(String className) throws ClassNotFoundException{
		 Class c = Class.forName(className);
		 Class[] interfaces = c.getInterfaces();
		 System.out.println("Implement interfaces:");
		 for(Class interface1:interfaces)
		 {
			 System.out.println(interface1.toString());
		 }
	}
	public static void getThePackageInfo(String className) throws ClassNotFoundException{
		 Class c = Class.forName(className);
		 Package p =c.getPackage();
		 System.out.println("The package is    :  " + p.toString());
	}
	public static void main(String args[]) throws ClassNotFoundException
	{
		System.out.println("Please input the class name:");
		Scanner scan = new Scanner(System.in);
		String className = scan.next();
		System.out.println("Input class name       :  "+className);
		//Analysis the fields.
		System.out.println("Filed info:");
		parseFiled(className);
		//Analysis the method. Not include super class's info

		System.out.println("Method info:");
		parseMethod(className);
		//Analysis the constructor
		System.out.println("Constructor info:");
		parseConstructor(className);
		//Analysis the super class
		System.out.println("SuperClss info:");
		getTheSuperClassInfo(className);
		//Analysis the class's interfaces
		System.out.println("Implement interface info:");
		getTheInterfaceInfo(className);
		//get package info
		System.out.println("Package info:");
		getThePackageInfo(className);
		
	}
}

 End!

 

 

 

 

你可能感兴趣的:(java,C++,c,C#)