4

嗨,我有以下 java 代码,

public class A{
private String B="test_string";
private int AA;
public int C;
private int method1()
{
    int a;
    a=0;
    return a;
}


private int method1(int c, String d)
{
    int a;
    a=c;
    return a;
}
}

但是当我使用 javap -c 命令来获取等效的字节码时,

    Compiled from "A.java"
public class A extends java.lang.Object{
public int C;

public A();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   aload_0
   5:   ldc #2; //String test_string
   7:   putfield    #3; //Field B:Ljava/lang/String;
   10:  return

}

这里的字节码我不清楚,因为私有变量和方法声明在哪里?

谁能给我解释一下?

4

1 回答 1

12

您需要-p显示私人成员的选项:

javap -c -p A

然后你会看到一切:

Compiled from "A.java"
public class A {
  private java.lang.String B;

  private int AA;

  public int C;

  public A();
    Code:
       0: aload_0       
       1: invokespecial #1        // Method java/lang/Object."<init>":()V
       4: aload_0       
       5: ldc           #2        // String test_string
       7: putfield      #3        // Field B:Ljava/lang/String;
      10: return        

  private int method1();
    Code:
       0: iconst_0      
       1: istore_1      
       2: iload_1       
       3: ireturn       

  private int method1(int, java.lang.String);
    Code:
       0: iload_1       
       1: istore_3      
       2: iload_3       
       3: ireturn       
}
于 2013-05-10T09:04:00.023 回答