有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

程序来查找java程序中的方法数

我使用以下代码来标识类中函数的数量。类似的方法可以帮助我识别java程序中的函数数量。在我的程序中,我将输入文件作为一个类。用代码指导我以java程序的形式提供输入,并找到其中声明的函数数

import java.lang.reflect.*;
import java.io.*;
import java.lang.String.*;
   public class Method1 {
      private int f1(
       Object p, int x) throws NullPointerException
      {
         if (p == null)
            throw new NullPointerException();
         return x;
      }

      public static void main(String args[])throws Exception
      {
         int Mcount=0,MthdLen=0;
         try {
           Class cls = Class.forName("Madhu");
        int a;
            Method methlist[]= cls.getDeclaredMethods();
            for (int i = 0; i < methlist.length;i++)
            {  
               Method m = methlist[i];
               Mcount = Mcount + 1;
               MthdLen=MthdLen+(m.getName().length());
            }
         }
         catch (Throwable e) {
            System.err.println(e);
         }
        System.out.println("Length = " + MthdLen);
        System.out.println("Mcount = " + Mcount);
      }
   }

共 (4) 个答案

  1. # 1 楼答案

    在JDK6+中,可以在运行时使用JavaCompiler编译文件,并使用旧代码查找方法的数量

    编辑:
    奖励:将您的代码替换为

    System.out.println("Total number of methods: " +  
                  java.beans.Introspector.getBeanInfo( //your class name here
                                                  ).getMethodDescriptors().length);
    
  2. # 2 楼答案

    我使用这段代码来找出另一个类中一个类中的方法计数

    public class test1 {
        public static void main(String[] args)
    //if use in class 
    //Method[] s = getClass.getDeclaredMethods();
            Method[] s = SmokeTestTests.class.getDeclaredMethods();
            int methodCounter = 0;
            for (Method method :
                    s) {
                ++methodCounter;
            }
            System.out.println(methodCounter);
        }
    
  3. # 3 楼答案

    首先,

     Class cls = Class.forName("Madhu");
    

    要求所需类的完全限定名例如Class.forName("java.lang.Thread")'

    第二,

     Method methlist[]= cls.getDeclaredMethods();
    

    仅返回该特定类的publicprotectedprivate和默认方法(它不包括继承的方法)

    第三,

    MthdLen=MthdLen+(m.getName().length());
    

    对方法名称的字符串长度求和。你需要这个做什么?您可以简单地进行如下计数:

    int MCount = cls.getDeclaredMethods().length; //If the "getDeclaredMethods()` doesn't return a null.
    

    最后,如果您需要获得所有继承的公共&;您可以使用该类的受保护方法

    Class<?> class2 = cls.getSuperClass();
    
    //Get all methods using
    Method[] methods2 = class2.getDeclaredMethods();
    
    //Iterate through methods2 and retrieve all public, protected methods and add it to MCount.
    

    希望这有帮助

  4. # 4 楼答案

    Java没有任何函数,所以答案是0

    如果你正在寻找一些方法,你必须问自己,你想吗

    • 包括继承的方法
    • 一次或多次计数重写的方法
    • 是否包含对象中的所有方法

    例如

    public class Main {
        static class A {
            public String toString() {
                return super.toString();
            }
        }
        static class B extends A {
            public String toString() {
                return super.toString();
            }
        }
    
        public static void main(String args[]) {
            for(Class clazz = B.class;clazz != null;clazz = clazz.getSuperclass()) {
                for(Method m: clazz.getDeclaredMethods())
                    System.out.println(m);
            }
        }
    }
    

    印刷品

    public java.lang.String Main$B.toString()
    public java.lang.String Main$A.toString()
    protected void java.lang.Object.finalize() throws java.lang.Throwable
    public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
    public final void java.lang.Object.wait() throws java.lang.InterruptedException
    public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
    public boolean java.lang.Object.equals(java.lang.Object)
    public java.lang.String java.lang.Object.toString()
    public native int java.lang.Object.hashCode()
    public final native java.lang.Class java.lang.Object.getClass()
    protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException
    private static native void java.lang.Object.registerNatives()
    public final native void java.lang.Object.notify()
    public final native void java.lang.Object.notifyAll()