java学习之反射机制

import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
 * java 反射机制学习
 * 542335496@qq.com
 * 2012-11-6
 * @author Administrator
 *
 */
class MyClass {
	
	private String myName;
	private int age;
	
	public String sayHello(String name){
		String ret="Hello,"+name+",My name is "+myName;
		return ret;
	}
	public String introduce(){
		String ret="My name is "+myName+",my age is"+age;
		return ret;
	}
}
class LoadedClass{
	
	static{
		System.out.println("类LoadedClass载入");
	}
	
	public LoadedClass(){
		
		System.out.println("类LoadedClass实例化");
	}	
}
interface MyInterface{
	void say();
	void say(String name);
}
class MyClass1 implements MyInterface{

	@Override
	public void say() {
		
		System.out.println("Hello,I'm MyClass1!");
	}

	@Override
	public void say(String name) {
		System.out.println("Hello,"+name+" I'm MyClass1!");
	}
	
}
class MyClass2 implements MyInterface{

	@Override
	public void say() {
		
		System.out.println("Hello,I'm MyClass2!");
	}

	@Override
	public void say(String name) {
		System.out.println("Hello,"+name+" I'm MyClass2!");
	}
	
}

class PersonInfo {
	
	public String name;
	public int age;
	public void say(){
		
		System.out.println("I'm "+name+",my age is"+age);
	}
	
}
 public class GetClassInfo{
	
	public static void main(String[] args){
		//testMyClass();
		//testLoadedClass();
		//testInterface("MyClass1");
		//testInterface("MyClass2");
		modifyField();
	}
	@SuppressWarnings("rawtypes")
	public static void modifyField(){
		try {
			Class<?> cls=Class.forName("PersonInfo");
			Object obj=cls.newInstance();
			Field field1=cls.getField("name");
			field1.set(obj, "wujianhua");
			
			Field field2=cls.getField("age");
			field2.setInt(obj, 22);
			
			Class[] para=null;
			Method method=cls.getMethod("say", para);
			Object[] args=null;
			method.invoke(obj, args);
			
		} catch (Exception e) {
			
			
		}
				
	}
	
	public  static void testInterface(String className){
		
		try {
			Class<?> cls=Class.forName(className);
			Object obj=cls.newInstance();
			System.out.println("类名"+cls.getName());
			Method method=cls.getDeclaredMethod("say", null);
			method.invoke(obj, null);
			System.out.println("========");
			Method method2=cls.getDeclaredMethod("say", String.class);
			method2.invoke(obj, "wujianhua");
			System.out.println("------");
			Method myMethod=cls.getMethod("say", new Class[]{String.class});
			myMethod.invoke(cls.newInstance(), new Object[]{"yunshouhu"});
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@SuppressWarnings("rawtypes")
	public static void testLoadedClass(){
		
		System.out.println("声明LoadedClass类型引用");
		LoadedClass lc;
		System.out.println("实例化LoadedClass");
		lc=new LoadedClass();
		lc.getClass();
		System.out.println("====");
		try {
			Class cls = Class.forName("LoadedClass");
			System.out.println("当前类是数组:" + cls.isArray());
			System.out.println("当前类是枚举:" + cls.isEnum());
			System.out.println("当前类是接口:" + cls.isInterface());
			System.out.println("当前类是本地类:"+cls.isLocalClass());
			System.out.println("当前类是一个实例:"+cls.isInstance(lc));
			System.out.println("当前类是匿名类:"+cls.isAnonymousClass());
			System.out.println("当前类是成员内部类:" + cls.isMemberClass());
			System.out.println("当前类是原始类:" + cls.isPrimitive());
			System.out.println("当前类是合成类:" + cls.isSynthetic());

		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		}
		
		
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void testMyClass(){
		Class cls=MyClass.class;
		System.out.println("类名:"+cls.getName());
		System.out.println();
		System.out.println("成员:");
		Field[] field=cls.getDeclaredFields();
		for(int i=0;i<field.length;i++){
			System.out.println(field[i]);
		}
		System.out.println("方法:");
		Method[] methods=cls.getDeclaredMethods();
		for(int j=0;j<methods.length;j++){
			System.out.print(methods[j].getReturnType().getName()+" "+methods[j].getName()+"(");
			Class[] paraCls=methods[j].getParameterTypes();
			for(int k=0;k<paraCls.length;k++){
				if(k>0){
					System.out.print(",");
				}
				System.out.print(paraCls[k].getName());
			}
			System.out.println(")");			
		}
		System.out.println("======");
		try {
			Method myMethod=cls.getMethod("sayHello", new Class[]{String.class});
			myMethod.invoke(cls.newInstance(), new Object[]{"yunshouhu"});
		} catch (Exception e) {
			e.printStackTrace();
		}  
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值