可以
用Reflection
看看thinking in java吧
Class类有getMethods和getConstructors函数,分别传回Method数组和Constructor数组
前者包含所有方法,后者包含所有构造函数的名称、参数、返回值等信息

解决方案 »

  1.   

    import java.lang.reflect.*;public class Test {
    public static void main(String[] args)
    throws ClassNotFoundException{

    Method[] method=Class.forName("java.awt.Graphics").getMethods();

    for(int i=0;i<method.length;i++){
    System.out.println (method[i].getName());
    }
    }
    }输出finalize
    toString
    setColor
    clearRect
    clipRect
    copyArea
    create
    create
    dispose
    draw3DRect
    drawArc
    drawBytes
    drawChars
    drawImage
    drawImage
    drawImage
    drawImage
    drawImage
    drawImage
    drawLine
    drawOval
    drawPolygon
    drawPolygon
    drawPolyline
    drawRect
    drawRoundRect
    drawString
    drawString
    fill3DRect
    fillArc
    fillOval
    fillPolygon
    fillPolygon
    fillRect
    fillRoundRect
    getClip
    getClipBounds
    getClipBounds
    getClipRect
    getColor
    getFont
    getFontMetrics
    getFontMetrics
    hitClip
    setClip
    setClip
    setFont
    setPaintMode
    setXORMode
    translate
    hashCode
    getClass
    wait
    wait
    wait
    equals
    notify
    notifyAll进而还可以得到方法的参数,就是整个方法的原型。
    得用反射
      

  2.   

    class.getDeclaredMethods()
    可以提取出所有的public的方法,private的是谁也提取不出来的
      

  3.   

    think in java2中提供了一个专门的工具,用于取出某各类的构造函数及方法
      

  4.   

    同意kypfos(社会主义好) ( )
      

  5.   

    private 通过一些特殊的类库也是可以取得的
      

  6.   

    回复人: lisonghua(天涯) ( ) 信誉:100  2004-01-02 10:58:00  得分:0 
     
     
      class.getDeclaredMethods()
    可以提取出所有的public的方法,private的是谁也提取不出来的
     说的不对 getDeclaredMethods
    public Method[] getDeclaredMethods()
                                throws SecurityException
    Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private methods, but excludes inherited methods.
    上面是jdk api里的原话
    注意最后一句:包含了public,protected,default,AND PRIVATE的方法
    私有的也是可以取出来的,但是不能显示继承自父类的method

    getMethods
    public Method[] getMethods()
                        throws SecurityException
    Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces.
    这个可以取出继承的方法,但只能取出public的方法
      

  7.   

    import java.util.regex.*;
    import java.lang.reflect.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */
    public class ShowMethods
    {
        private static final String usage = "usage:\n" +
                                            "ShowMethods qualified.class.name\n" +
                                            "to show all methods in class or:\n" +
                                            "ShowMethods qualified.class.name word\n" +
                                            "to search for methods involving 'word'";
        private static Pattern p = Pattern.compile("\\w+\\.");
        public ShowMethods()
        {
        }    public static void main(String[] args)
        {
            if (args.length < 1)
            {
                System.out.println(usage);
                System.exit(0);
            }
            //System.out.println(args[0]);        int lines = 0;
            ShowMethods showMethods1 = new ShowMethods();
            try
            {
                Class c = Class.forName(args[0]);
                Method[] m = c.getMethods();
                Constructor[] ctor = c.getConstructors();
                if (args.length == 1)
                {
                    for (int i = 0; i < m.length; i++)
                    {
                        System.out.println(p.matcher(m[i].toString()).replaceAll(""));
                        //System.out.println(m[i].toString());
                    }
                    for (int i = 0; i < ctor.length; i++)
                    {
                        System.out.println(p.matcher(ctor[i].toString()).replaceAll(""));
                        //System.out.println(ctor[i].toString());
                    }
                    lines = m.length + ctor.length;
                }
                else
                {
                    for (int i = 0; i < m.length; i++)
                    {
                        if (m[i].toString().indexOf(args[1]) != -1)
                        {
                            System.out.println(p.matcher(m[i].toString()).replaceAll(""));
                            lines++;
                        }
                    }
                    for (int i = 0; i < ctor.length; i++)
                    {
                        if (ctor[i].toString().indexOf(args[i]) != -1)
                        {
                            System.out.println(p.matcher(ctor[i].toString()).replaceAll(""));
                            lines++;
                        }
                    }
                }
            }
            catch (ClassNotFoundException e)
            {
                System.err.println("No such class found " + e);
            }
        }
    }
    Thinking in java中的一个例程,比较简单,看一看吧.他能够输出你作为参数传入的类里所有的方法和构造函数的信息.
      

  8.   

    用 kypfos(社会主义好) 的方法测试一下就看出来结果了:
    测试类:
    //temp.java
    class temp 
    {
    private void pp(){}
    public void ll(){}
    protected void aa(){}
    void ss(){}
    }//用getMethods
    //Test.java
    import java.lang.reflect.*;public class Test {
    public static void main(String[] args)
    throws ClassNotFoundException{

    Method[] method=Class.forName("java.awt.Graphics").getMethods();

    for(int i=0;i<method.length;i++){
    System.out.println (method[i].getName());
    }
    }
    }
    输出:
    ---------- Run Java ----------
    ll
    hashCode
    getClass
    wait
    wait
    wait
    equals
    toString
    notify
    notifyAll输出完成 (耗时 0 秒) - 正常终止//用getDeclaredMethods
    //Test.java
    import java.lang.reflect.*;public class temp {
    public static void main(String[] args)
    throws ClassNotFoundException{

    Method[] method=Class.forName("testforreflection").getDeclaredMethods();
    for(int i=0;i<method.length;i++){
    System.out.println (method[i].getName());
    }
    }
    }
    运行结果:
    ---------- Run Java ----------
    pp
    ll
    aa
    ss输出完成 (耗时 0 秒) - 正常终止
      

  9.   

    上面测试的
    Method[] method=Class.forName("java.awt.Graphics").getMethods();
    Method[] method=Class.forName("testforreflection").getDeclaredMethods();
    这两句要改掉,我自己测试起的类名不是temp
    改成Method[] method=Class.forName("temp").getMethods();
    Test.java和temp.java放一个目录下,先编译temp.java就可以了
      

  10.   

    看看Struts的DispatchAction
    里面就有取出方法的代码 将“流氓无赖”测试到底
    ——始于2003年7月
    树欲止而风不停,行云流水匆匆去;
    树梢蚂蚱凭空望,江边浪花碎巨石; 支持“流金岁月”!!!
    发送框,少个“右键菜单,选择粘贴”;
    ——2003年12月24日am^@^