`
hilary3113
  • 浏览: 262930 次
  • 性别: Icon_minigender_1
  • 来自: 邯郸
社区版块
存档分类
最新评论

java反射机制

阅读更多

 

package com.hilary;

import java.lang.reflect.Method;

/**
 * @Author: hilary
 * @Date: 2011-7-26
 **/
public class Fanshe {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Class cls = Class.forName("com.hilary.MyObject");
			Method[] methods = cls.getDeclaredMethods();
			System.out.println(methods.length);
			for(int i = 0; i < methods.length; i++){
				Method method = methods[i];
				System.out.println("方法名:" + method.getName());
				System.out.println("修饰符:" + method.getModifiers());
				System.out.println("返回值:" + method.getReturnType());
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

class MyObject {
	public int a;
	public int b;
	
	public MyObject(int _a, int _b){
		a= _a;
		b= _b;
	}
	
	public int sum(){
		return a+b;
	}
	
	public int minus(){
		return a-b;
	}
	
	public int multiply(){
		return a*b;
	}
	
	public int divide(){
		return a/b;
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics