2017-07-21 30 views
0

這是我想將類實例:無法實例使用參數的構造函數的對象的Java

public class GoogleSheetsAPI { 

String spreadsheetId; 
Sheets service; 
String credentialsFile; 

public void GoogleSheetsAPI() { 
} 

public void GoogleSheetsAPI(String spreadsheetId, String credentialsFile) throws Exception { 
    this.spreadsheetId = spreadsheetId; 
    this.credentialsFile = credentialsFile; 
    service = getSheetsService(); 
} 

}

這是我正在創建該類的對象GoogleSheetsAPI

GoogleSheetsAPI googleSheetsAPI = new GoogleSheetsAPI(spreadsheetId, credentiialsFile); 

enter image description here

+0

我沒有看到這裏的任何參數代碼;你明白這個詞的意思嗎?已移除標記。 –

+0

@Abhijit你叫什麼** spreadsheetId,credentiialsFile **? – Rameshwar

+0

形式參數。 https://chortle.ccsu.edu/java5/Notes/chap34A/ch34A_3.html –

回答

1

的構造不得有void,它應該是:

public GoogleSheetsAPI(String spreadsheetId, String credentialsFile) throws Exception { 
    this.spreadsheetId = spreadsheetId; 
    this.credentialsFile = credentialsFile; 
    service = getSheetsService(); 
} 
+0

這是我犯的錯誤。謝謝Damian :)構造函數沒有void。 – Rameshwar

-1

Class.newInstance調用無參數構造

Class<?> cl = Class.forName("javax.swing.JLabel"); 
Constructor<?> cons = cl.getConstructor(String.class); 

對象o = cons.newInstance( 「JLabel的」);

要調用不同的構造函數,需要使用反射包(java.lang.reflect)。

0

構造函數沒有任何返回類型......因爲是一種特殊的方法,如果你定義自己的構造則沒有定義空的構造函數,因爲JVM提供默認...

相關問題