java如何返回类的方法名称及其参数
如何返回类的方法,当然使用大名鼎鼎的反射机制。
// Class类:
1.public Method[] getDeclaredMethods()
throws SecurityException; // 返回被反射的类中声明的方法数组;
2.public Method[] getMethods()
throws SecurityException;// 返回所有方法,包含继承自父类的方法;
// Method类:
public final class Method
extends AccessibleObject
implements GenericDeclaration, Member ;// 方法类的定义;
1.public Class>[] getParameterTypes(); // 方法参数表
2.public int getModifiers(); // 修饰符
3.public Class> getReturnType();// 返回值类型
4.public String getName(); // 方法名
5.public Class>[] getExceptionTypes();// 返回方法抛出的异常
——实例:
package com.mldn;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier; // 修饰符定义类
public class GetMethods
{
public static void main(String[] args)
{
Class> c = null; // 声明Class对象
try
{
c = Class.forName(“com.mldn.Person”); // 获取T类的反射
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
//Method[] cons = c.getDeclaredMethods(); // 取得所有本类中声明的方法
Method[] cons = c.getMethods(); // 取得所有方法
for (Method c1 : cons)
{
Class>[] para = c1.getParameterTypes(); // 1.获取当前方法的参数表
System.out.print(“方法: “);
int mo = c1.getModifiers(); // 2.获取当前方法的修饰符(访问权限)
System.out.print(Modifier.toString(mo) + ” “);
System.out.print(c1.getReturnType().getName() + ” “); // 3.获取返回值类型
System.out.print(c1.getName() + “(“); // 4.打印方法名
int i = 0;
for (Class> p : para)
{
System.out.print(p.getName() + ” arg” + i);
if (++i < para.length)
{
System.out.print(“, “); // 若不是最后一个参数打印逗号
}
}
Class>[] ex = c1.getExceptionTypes(); // 5.获取当前方法抛出的异常
if (ex.length > 0) // 若存在异常
{
System.out.print(“) throws “);
for (Class> e : ex) // 打印异常
{
System.out.print(e.getName() + ” “);
}
System.out.println(); // 换行
}
else
{
System.out.println(“){…}”); // 换行
}
}
}
}
interface Chinese
{
public static final String NATIONAL = “China”; // 全局常量
public void sayChina(); // 抽象方法
public void sayHello();
}
class Person implements Chinese
{
private String name = null;
private int age = 0;
public Person() // 无参构造
{}
public Person(String name, int age)
{ // 有参构造
this.name = name;
this.age = age;
}
public void sayChina() // 覆写
{
System.out.println(“China,I’m Chinese!”);
}
public void sayHello() // 覆写
{
System.out.println(“Hello, World!”);
}
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public String toString()
{
return “姓名:” + this.name + ” 年龄:” + this.age;
}
}
/* 这里只是通过Method类的方法打印方法的详细信息
ubuntu@xu-desktop:~$ java com.mldn.GetMethods // 本类中定义的方法:
方法: public void sayChina(){…}
方法: public void sayHello(){…}
方法: public int getAge(){…}
方法: public void setAge(int arg0){…}
方法: public java.lang.String toString(){…}
方法: public java.lang.String getName(){…}
方法: public void setName(java.lang.String arg0){…}
——-// 本类中定义以及继承父类的方法:
ubuntu@xu-desktop:~$ java com.mldn.GetMethods
方法: public void sayChina(){…}
方法: public void sayHello(){…}
方法: public int getAge(){…}
方法: public void setAge(int arg0){…}
方法: public java.lang.String toString(){…}
方法: public java.lang.String getName(){…}
方法: public void setName(java.lang.String arg0){…}
方法: public final native void wait(long arg0) throws java.lang.InterruptedException
方法: public final void wait(long arg0, int arg1) throws java.lang.InterruptedException
方法: public final void wait() throws java.lang.InterruptedException
方法: public native int hashCode(){…}
方法: public final native java.lang.Class getClass(){…}
方法: public boolean equals(java.lang.Object arg0){…}
方法: public final native void notify(){…}
方法: public final native void notifyAll(){…}
*/
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《java如何返回类的方法名称及其参数》
本文地址:http://www.xiupu.net/archives-141.html
关注公众号:
微信赞赏
支付宝赞赏