我的Java Error构造函数在类中不能应用于给定的类型;

0 java bluej

我是初学者,正在努力编写我的作品.但它不起作用.我收到这个错误

"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..."
Run Code Online (Sandbox Code Playgroud)

如果有人能向我解释这将是非常感谢.

Pio*_*ski 5

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

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

Account account = new Account();  // ok
Account account = new Account("some name");  // ok
Run Code Online (Sandbox Code Playgroud)