我使用下面的代碼來確定一個類中函數的數量。類似的方法可以幫助我確定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);
}
}
會出現這種情況是功課? – 2011-03-28 15:21:29
「將輸入作爲Java程序」是什麼意思?你想要計算什麼? – 2011-03-28 15:38:44