java反射机制--练习

利用java的放射机制分析类,包括构造函数,方法、域(成员变量)


1.类

Demo.java

package com.wei;

public class Demo
{
	private int age;
	public String i="null";
	
	private  Demo()
	{
		
	}
	
	public  Demo(int age)
	{
		this.age = age;
	}
	
	public String toString()
	{
		return "["+this.getClass()+" age="+this.age+" i="+this.i+"]";
	}
	
	private void xxx()
	{
		
	}

}


2.利用反射分析类

ReflectionTest.java

package com.wei;

import java.lang.reflect.*;
import java.util.*;

/*利用反射来分析类*/
public class ReflectionTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		String name;
		if(args.length>0)
		{
			name = args[0];
		}
		else
		{
			System.out.println("输入类的名字,例如:java.lang.String ");
			Scanner in = new Scanner(System.in);
			name = in.next();
			String s = "类名输入完成!以下是类的分析结果:";
			System.out.printf("%s\n",s);
			
		}
		
		try
		{
			Class c1 = Class.forName(name);
			Class superc1 = c1.getSuperclass();
			//打印 类的修饰符 public private final static...
			String modifiers = Modifier.toString(c1.getModifiers());
			if(modifiers.length()>0) 
			{
				System.out.print(modifiers+" "); 
			}
			//打印 类名
			System.out.print("class "+name); 
			//打印 类的超类
			if(superc1 !=null && superc1 != Object.class)
			{
				System.out.print(" extends "+superc1.getName());
			}
			
			System.out.print("\n{\n"); 
			
			printConstractors(c1);
			System.out.println();
			printMethods(c1);
			System.out.println();
			printFields(c1);
			
			System.out.print("\n}\n"); 
			
		} 
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.exit(0);
		
	}

	   //方法函数
	   public static void printMethods(Class cl)
	   {
	      Method[] methods = cl.getDeclaredMethods();

	      for (Method m : methods)
	      {
	         Class retType = m.getReturnType();
	         String name = m.getName();

	         System.out.print("  ");
	         // print modifiers, return type and method name
	         String modifiers = Modifier.toString(m.getModifiers());
	         if (modifiers.length() > 0) 
	         {
	         	System.out.print(modifiers + " ");         
	         }
	         System.out.print(retType.getName() + " " + name + "(");

	         // print parameter types
	         Class[] paramTypes = m.getParameterTypes();
	         for (int j = 0; j < paramTypes.length; j++)
	         {
	            if (j > 0) System.out.print(", ");
	            System.out.print(paramTypes[j].getName());
	         }
	         System.out.println(");");
	      }
	   }

	
	//域
	private static void printFields(Class c1)
	{
		// TODO Auto-generated method stub
		//	Field[] fields = c1.getFields();
		Field[] fields = c1.getDeclaredFields();

		for (Field f : fields)
		{
			//域的类型----int double...
			Class type = f.getType();
			//域的名字 XXX...
			String name = f.getName();
			System.out.print("  ");
			//打印域的修饰符---public private final static...
			String modifier = Modifier.toString(f.getModifiers());
			if(modifier.length()>0) 
			{
				System.out.print(modifier+" "); 
			}
			//打印 域的类型和名字 ---int SIZE...
			System.out.println(type.getName()+" "+name+";"); 
			
		}
	}

	//构造函数
	private static void printConstractors(Class c1)
	{
		// TODO Auto-generated method stub
		Constructor[] constructors = c1.getDeclaredConstructors();
		for (Constructor c : constructors)
		{
			String name = c.getName();
			System.out.print("  ");
			//打印 类的修饰符 public private final static...
			String modifier = Modifier.toString(c.getModifiers());
			if(modifier.length()>0) 
			{
				System.out.print(modifier+" "); 
			}
			System.out.print(name+"("); 
			
			//构造函数的参数
			Class[]  paramTypes = c.getParameterTypes();
			for(int j=0; j<paramTypes.length; j++)
			{
				if(j>0) System.out.print(", ");
				System.out.print(paramTypes[j].getName()); 
			}
			System.out.print(");\n"); 
		}
		
	}

	
}


3.结果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值