gpt4 book ai didi

java - java中如何统计变量和方法的个数

转载 作者:行者123 更新时间:2023-12-01 09:29:43 27 4
gpt4 key购买 nike

我有一个 java 文件 a.java 包含

int ab( fg)
abs
bcd
abs
int x,y;

现在我想区分 int 变量和 int 方法,并将其存储在不同的数组中并将输出作为变量:a b 和方法:ab 但我很困惑如何区分两者..

class ClassDemo {
public static void main(String args[]) {
BufferedReader br = null;
try {
String intarray[] = new String[50]; /*declaring array for int */
String line, a, str = null;
char str1 = 0; /*declaring char str1*/
char str2[] = new char[20];
char str3 = 0; /*declaring char str1*/
char str4[] = new char[20];
br = new BufferedReader(new FileReader("c:/java/a.java")); /*loading file*/
while ((line = br.readLine()) != null) /*reading file*/ {
StringTokenizer stringTokenizer = new StringTokenizer(line); /*spliting the line into string*/
while (stringTokenizer.hasMoreElements()) /*checking more elememts*/

{
str = stringTokenizer.nextElement().toString();
if (str.equals("int"))/*compare for int*/ {

while (str1 != '(') {
str = stringTokenizer.nextElement().toString();
for (int i = 0; i < str.length(); i++) {
str1 = str.charAt(i);
str2[i] = str1;
System.out.println(str2[i]);
}
}
while (str3 != ';') {
str = stringTokenizer.nextElement().toString();
for (int i = 0; i < str.length(); i++) {
str3 = str.charAt(i);
str4[i] = str3;
System.out.println(str4[i]);
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

最佳答案

java中的Reflection允许您在运行时获取有关类的信息。Reflection包有多个类用于方法/字段/接口(interface)等。这里有一个例子......

import java.lang.reflect.Method;
import java.lang.reflect.Field;
class DummyClass
{
public int x;
public int z;
public int meathod1()
{ return 1;
}
public int meathod2()
{ return 2;
}
public int method3()
{ return 3;
}
}

class MainClass
{
public static void main(String...s)
{
//Step 1: Getting the object of the class whose Fields/method u want to get.

DummyClass obj=new DummyClass();

//Step 2. Getting the Class of the class whose field/method u want to get.You can skip Step 1 if u already have Object the class.

Class myClass=obj.getClass();

//Step 3.Class has some multiple inbuilt methods to get the methods and Fields of any class.

Method[] methodList=myClass.getDeclaredMethods();


/*Now u have all the Methods you have declared in the class.The length of the methodList is the no of declared methods u have in your class.If u want to get the inherited methods too,then use getMethods() instead of getDeclaredMethods(),but then all your methods must be public then because getMethods() returns only public methods.*/
System.out.println("Total No of Methods : "+methodList.length);

//Step 4. Class has some other Inbuild methods that return Fields.

Field[] fieldList=myClass.getDeclaredFields();
System.out.println("Total no of Fields : "+fieldList.length);
}
}

关于java - java中如何统计变量和方法的个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39546011/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com