Java - 反射提取类

发布于 2025-01-06 19:28:51 字数 2377 浏览 0 评论 0原文

这是我的文件(已更新):

     import java.lang.reflect.*;

public class ClassExtract {

   public void printClassDefinition (String[] args){
       try {
            Class cls = Class.forName(args[0]);
            Field fieldlist[] 
              = cls.getDeclaredFields();
            for (int i 
              = 0; i < ((fieldlist.length + 1) - (fieldlist.length)); i++) 
            {
                Field fld = fieldlist[i];
                System.out.println("+ " + fld.getDeclaringClass().toString().replaceAll("class", ""));
                System.out.println("------");
            }                              
               for (int j = 0; j < fieldlist.length; j++) {
                   Field fld1 = fieldlist[j];
                   int mod = fld1.getModifiers();
                   System.out.println(Modifier.toString(mod).toString().replaceAll("public", "+") 
                           .replaceAll("private", "-").replaceAll("protected", "#") + " " + 
                           fld1.getType() + " " + fld1.getName());

               }
            }
          catch (Throwable e) {
             System.err.println(e);
          }

   }
  public static void main(String args[])
  {
      new ClassExtract().printClassDefinition(args);

   }
}

我应该读取这个文件:并且

    public class Complex{
    private int re;
    private int im;

    public Complex(int re, int im){
    this.re = re;
    this.im = im;
         }


    public Complex add(Complex h){
            return new Complex(this.re+h.re, this.im+h.im);
        }



    public Complex sub(Complex h){
    return new Complex(this.re-h.re, this.im-h.im);
        }

    public String toString(){
        if (im >= 0){
            return re +" + " +im + "i";
        }
        else return re +"  " +im + "i";
    }

    public static void main(String[] args){

    Complex c1 = new Complex(5,1);
    Complex c2 = new Complex(5,0);

    System.out.printf("c1 = %s, c2 = %s\n", c1, c2);
    System.out.printf("c1 + c2 = %s\n", c1.add(c2) );
     }}

它应该生成类似的内容:

    (+) Complex
     ***
    (-) int re
    (-) int im
     ***
    (+) Complex(re:int, im:int)
    (+) add():Complex
    (+) sub():Complex
    (+) toString():String
    (+) main()

由于我的文件迭代,因此我在该行之后得到了“re”和“int”的打印,以及我的格式总的来说是一团糟。我可以使用所有可能的帮助。谢谢。

This is my file (UPDATED):

     import java.lang.reflect.*;

public class ClassExtract {

   public void printClassDefinition (String[] args){
       try {
            Class cls = Class.forName(args[0]);
            Field fieldlist[] 
              = cls.getDeclaredFields();
            for (int i 
              = 0; i < ((fieldlist.length + 1) - (fieldlist.length)); i++) 
            {
                Field fld = fieldlist[i];
                System.out.println("+ " + fld.getDeclaringClass().toString().replaceAll("class", ""));
                System.out.println("------");
            }                              
               for (int j = 0; j < fieldlist.length; j++) {
                   Field fld1 = fieldlist[j];
                   int mod = fld1.getModifiers();
                   System.out.println(Modifier.toString(mod).toString().replaceAll("public", "+") 
                           .replaceAll("private", "-").replaceAll("protected", "#") + " " + 
                           fld1.getType() + " " + fld1.getName());

               }
            }
          catch (Throwable e) {
             System.err.println(e);
          }

   }
  public static void main(String args[])
  {
      new ClassExtract().printClassDefinition(args);

   }
}

and I am supposed to read this file:

    public class Complex{
    private int re;
    private int im;

    public Complex(int re, int im){
    this.re = re;
    this.im = im;
         }


    public Complex add(Complex h){
            return new Complex(this.re+h.re, this.im+h.im);
        }



    public Complex sub(Complex h){
    return new Complex(this.re-h.re, this.im-h.im);
        }

    public String toString(){
        if (im >= 0){
            return re +" + " +im + "i";
        }
        else return re +"  " +im + "i";
    }

    public static void main(String[] args){

    Complex c1 = new Complex(5,1);
    Complex c2 = new Complex(5,0);

    System.out.printf("c1 = %s, c2 = %s\n", c1, c2);
    System.out.printf("c1 + c2 = %s\n", c1.add(c2) );
     }}

and it should generate something like:

    (+) Complex
     ***
    (-) int re
    (-) int im
     ***
    (+) Complex(re:int, im:int)
    (+) add():Complex
    (+) sub():Complex
    (+) toString():String
    (+) main()

Since my file iterates through i get a print of "re" and "int" after the line for example, and my format is in general messed up. I could use all possible help. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2025-01-13 19:28:51

抱歉,伙计,恐怕您的代码与解决方案相差很远,向您发送正确的答案就像作弊一样......不过,我会推荐您使用以下方法。

  1. 创建一个名为 printClassDefinition (Class clazz) 的函数,这将打印输出的第一行,完成此操作后,创建一个仅运行这部分代码的 main 方法,运行几次并确保此函数本身会产生正确的输出。
  2. 创建一个名为 printFields 的函数(clazz 类)。此类循环遍历类的所有字段并仅打印字段的名称。
  3. 在您在 1 中创建的 main 中,为分隔行添加打印语句,并调用您在步骤 2 中创建的函数。与步骤 1 一样,练习您的代码并确保到目前为止您对结果感到满意。
  4. 创建一个名为 printField (Field) 的函数 在 printFields 循环内,用 println 函数替换对 printField 的调用。
  5. 现在让 printField 函数打印每个字段的正确输出。
  6. 再次练习你的 main,你的类和字段输出在这个阶段应该是准确的。
  7. 与2类似,创建printMethods(clazz类)。将此调用添加到您的 main 中,然后进行练习。
  8. 与4类似,创建printMehod(Method方法)并替换printMethods中的println。就是这样!

Sorry man, I am afraid your code is miles away from the solution, to send you the correct answer would be like cheating... I would recommend you the following approach though.

  1. Create a function called printClassDefinition (Class clazz) This would print the first line of your otuput, once that you complete this, create a main method that only runs this part of your code, run it a couple of times and make sure that this function by itself produces the right output.
  2. Create a function called printFields (Class clazz). This class loops through all the fields of your class and prints the name of the field only.
  3. In the main you created in 1, add a print statement for your separator line, and call the function you have created in step 2. As in step 1, exercise your code and make sure that so far, you are happy with the results.
  4. Create a function called printField (Field) Inside the loop of printFields, substitute your println function for a call to printField.
  5. Now make your printField function print the correct output for each field.
  6. Excercise again your main, your class and fields output should be spot on at this stage.
  7. Similar to 2, create printMethods (Class clazz). Add this call to your main, and exercise.
  8. Similar to 4, create printMehod (Method method) and substitute println in printMethods. And that's it!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文