java - 无法使用Java中的参数化构造方法实例化对象

标签 java constructor compiler-errors

这是我要实例化的类(class):

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

最佳答案

构造函数不得具有void,应为:

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

关于java - 无法使用Java中的参数化构造方法实例化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45229594/

相关文章:

java - 仅打印 JTable 中带有 Logo 的填充数据

java - 如何修复和执行此示例 Hibernate 程序以创建表

c++ - 从临时构造对象

c# - 使用接口(interface)对字典条目类型的正确性进行编译时检查

c++ - 为什么定义了之后还是有 "Identifier is undefined error "?

actionscript-3 - 奇怪的Flash编译器错误

java - JCDK 3.0.5u1中找不到eclipse-plugin文件夹?

java - 尝试创建演示者时出现辅助注入(inject)错误

java - BroadcastReceiver 在 android list 中没有默认构造函数

c++ - 什么时候在 C++ 中调用模板类的静态成员的构造函数?