程序来查找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);
}
}
# 1 楼答案
在JDK6+中,可以在运行时使用JavaCompiler编译文件,并使用旧代码查找方法的数量
编辑:
奖励:将您的代码替换为
# 2 楼答案
我使用这段代码来找出另一个类中一个类中的方法计数
# 3 楼答案
首先,
要求所需类的完全限定名例如
Class.forName("java.lang.Thread")'
第二,
仅返回该特定类的
public
、protected
、private
和默认方法(它不包括继承的方法)第三,
对方法名称的字符串长度求和。你需要这个做什么?您可以简单地进行如下计数:
最后,如果您需要获得所有继承的公共&;您可以使用该类的受保护方法
希望这有帮助
# 4 楼答案
Java没有任何函数,所以答案是0
如果你正在寻找一些方法,你必须问自己,你想吗
例如
印刷品