2016-09-09 63 views
0

我有一個非常簡單的類:瞭解javap的輸出

package MyDev; 

public class Point3D { 

    public Point3D(){x = 0; y = 0;z = 0;} 

    public float x, y, z; 
    public Point3D(float X, float Y, float Z) 
    { 
     x = X; y = Y; z = Z; 
    } 
} 

當我上這個班運行javap -c,這就是我得到:

C:\Projects\Development\MyJava\bin\MyDev>"C:\Program Files (x86)\Java\jdk1.7.0_55\bin\javap" -c Point3D.class 
Compiled from "Point3D.java" 
public class MyDev.Point3D { 
    public float x; 

    public float y; 

    public float z; 

    public MyDev.Point3D(); 
    Code: 
     0: aload_0 
     1: invokespecial #12     // Method java/lang/Object."<init>":()V 
     4: aload_0 
     5: fconst_0 
     6: putfield  #14     // Field x:F 
     9: aload_0 
     10: fconst_0 
     11: putfield  #16     // Field y:F 
     14: aload_0 
     15: fconst_0 
     16: putfield  #18     // Field z:F 
     19: return 

    public MyDev.Point3D(float, float, float); 
    Code: 
     0: aload_0 
     1: invokespecial #12     // Method java/lang/Object."<init>":()V 
     4: aload_0 
     5: fload_1 
     6: putfield  #14     // Field x:F 
     9: aload_0 
     10: fload_2 
     11: putfield  #16     // Field y:F 
     14: aload_0 
     15: fload_3 
     16: putfield  #18     // Field z:F 
     19: return 
} 

爲什麼? 爲什麼第二個構造函數也有簽名()V,而不是(FFF)V? 對於我的生活我無法理解!

+0

Object上沒有'(float,float,float)'構造函數。 –

回答

4

這是對構造函數Object的調用。對超類構造函數的調用始終是構造函數中發生的第一件事。

+0

對不起,我的錯,應該用-c -s來運行,以使事情更清晰。答案已被接受並被提升。 – Flot2011