【问题标题】:My Java Error constructor, in class, cannot be applied to given types;类中的我的 Java 错误构造函数不能应用于给定类型;
【发布时间】:2025-12-11 04:00:02
【问题描述】:

我是一个初学者,正在尝试编译我的作品。但是它不起作用。我收到此错误

"constructor account in class account cannot be applied to given types;
required: in,java,lang,String; found: no arguments; reason: actual and formal argument lists differ in..."

如果有人能向我解释这一点,将非常感激。

【问题讨论】:

  • 发布您尝试过的代码
  • 最有可能的是,构造函数接收到一个String 参数,但被调用时根本没有任何参数。但是,查看代码会澄清这一点。
  • 请发布帐户类代码和您尝试创建帐户实例的代码。

标签: java bluej


【解决方案1】:

这很可能意味着您忘记将参数传递给构造函数。

class Account {
    Account(String name) {
      // ....
    }
} 

// somewhere in the code:
Account account = new Account();  // invalid, no arguments found, java.lang.String needed
Account account = new Account("some name");  // ok

请注意,在 Java 中,当您添加带参数的构造函数时,默认的无参数构造函数不会自动生成,您必须自己提供:

class Account {
    Account() {   
      // ....
    }

    Account(String name) {
      // ....
    }
} 

Account account = new Account();  // ok
Account account = new Account("some name");  // ok

【讨论】:

    最近更新 更多