Javs 反射 之 取得全部方法

package com.cn.service;

public interface China {

	public static final String NATIONAL="China";
	
	public static final String AUTHOR="老李";
	
	public void sayChina();
	
	public String sayHello(String name,int age);
}


package com.cn.model;

import com.cn.service.China;

public class Person implements China{

	private String name;
	
	private int age;
	
	public Person(){
		
	}
	
	public Person(String name){
		this.name=name;
	}
	
	public Person(String name,int age){
		this(name);//调用有一个参数的构造函数
		this.setAge(age);
	}
	
	public void sayChina() {
		// TODO Auto-generated method stub
		System.out.println("作者:"+AUTHOR+" ,国籍: "+NATIONAL);
	}

	public String sayHello(String name, int age) {
		// TODO Auto-generated method stub
		return name+",你好!我今年"+age+"岁了";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	

}


package com.cn.component;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * 获取一个类的全部方法
 * @author Administrator
 *
 */
public class GetMethodDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Class<?> c=null;//申明class对象,泛型
		try{
			c=Class.forName("com.cn.model.Person");//实例化class对象
		}
		catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		Method m[]=c.getMethods();//获取类Person的全部方法
		for(int i=0;i<m.length;i++){
			int mo=m[i].getModifiers();//获取修饰符数字
			String priv=Modifier.toString(mo);//获取修饰符
			System.out.print(priv+"  ");
			Class<?> type=m[i].getReturnType();//获取返回值类型
			System.out.print(type.getName()+"  ");//取得返回类型名称
			
			System.out.print(m[i].getName()+" ");//方法名称
			System.out.print("(");//参数左边括号
			Class<?> pt[]=m[i].getParameterTypes();//获取参数类型
			for(int j=0;j<pt.length;j++){
				System.out.print(pt[j].getName()+" "+"arg"+j);//取得参数类型名称,如:int ,或者String 
				if(j<pt.length-1){
					System.out.print(",");
				}
			}
			Class<?> ex[]=m[i].getExceptionTypes();//获取异常类型
			if(ex.length>0){
				System.out.print(") throws ");
			}
			else{
				System.out.print(" )");
			}
			for(int k=0;k<ex.length;k++){
				System.out.print(ex[k].getName());//输出异常信息名称
				if(k<ex.length-1){
					System.out.print(" ,");
				}
			}
			System.out.println();//换行
		}
		
	}

}
//输出结果,不仅输出person类中的方法,也输出了从Object类中继承的方法
//public  void  setAge (int arg0 )
//public  void  sayChina ( )
//public  java.lang.String  sayHello (java.lang.String arg0,int arg1 )
//public  int  getAge ( )
//public  java.lang.String  getName ( )
//public  void  setName (java.lang.String arg0 )
//public native  int  hashCode ( )
//public final native  java.lang.Class  getClass ( )
//public final  void  wait (long arg0,int arg1) throws java.lang.InterruptedException
//public final  void  wait () throws java.lang.InterruptedException
//public final native  void  wait (long arg0) throws java.lang.InterruptedException
//public  boolean  equals (java.lang.Object arg0 )
//public  java.lang.String  toString ( )
//public final native  void  notify ( )
//public final native  void  notifyAll ( )

 

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