繁体   English   中英

我的Java Error构造函数在类中无法应用于给定的类型。

[英]My Java Error constructor, in class, cannot be applied to given types;

我是初学者,正在尝试汇编我的作品。 但是,它不起作用。 我得到这个错误

"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..."

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM