我剛開始學習Spring框架,並在javatpoint.com上找到了一些教程。Java - 在給定示例中使用super()
我有了這個代碼(沒什麼特別的,只打印了一些問題和答案):
private int id;
private String name;
private Map<Answer,User> answers;
public Question(){}
public Question(int id, String name, Map<Answer,User> answers){
super();
this.id = id;
this.name = name;
this.answers = answers;
}
我的問題是:爲什麼他使用空的構造和關鍵字超() ?該應用程序沒有他們,我不明白他們在這段代碼中的優點。
P.S. :沒有超級類或類似的東西。
沒有必要顯式調用'super()'。如果省略,編譯器會插入它(前提是超級類沒有參數,否則必須顯式調用它)。 –
@AndyTurner所以不需要使用'super()'。但是空構造函數Question()'怎麼樣? –
它將確保可以在不傳遞參數的情況下創建對象 –