Java 找不到符号-构造函数项()

Java 找不到符号-构造函数项(),java,constructor,symbols,default-constructor,Java,Constructor,Symbols,Default Constructor,考虑以下代码: // Create a Item oject item item = new item(); 编译器错误消息: 错误-找不到符号-构造函数项 类别项目: 这段代码之后是属性的所有setter/getter方法 如何解决此问题?创建参数化构造函数时,除非创建自己的构造函数,否则默认构造函数将被删除 因此,必须显式创建默认构造函数,如下所示: public Item() {} 您需要添加默认构造函数: public item() { // any initializatio

考虑以下代码:

// Create a Item oject
item item = new item();
编译器错误消息:

错误-找不到符号-构造函数项

类别项目:

这段代码之后是属性的所有setter/getter方法


如何解决此问题?

创建参数化构造函数时,除非创建自己的构造函数,否则默认构造函数将被删除

因此,必须显式创建默认构造函数,如下所示:

public Item() {}

您需要添加默认构造函数:

public item() {
   // any initialization you need here
}
public item(String ItemcodeIn, String ItemnameIn, String DescriptionIn, 
     String StyleIn, String FinishIn, float UnitpriceIn, float StockIn, 
     String SuppliercodeIn, String SuppliernameIn, String AddressIn)
public Item() {
    // Some code goes here
}
您还应根据最佳实践将类重命名为Item capital I,字段应以小写字母开头。

您的Item类只有一个构造函数:

public item() {
   // any initialization you need here
}
public item(String ItemcodeIn, String ItemnameIn, String DescriptionIn, 
     String StyleIn, String FinishIn, float UnitpriceIn, float StockIn, 
     String SuppliercodeIn, String SuppliernameIn, String AddressIn)
public Item() {
    // Some code goes here
}
尽管您试图通过新项目访问它;此构造函数不存在,因为您使用参数化的构造函数覆盖了它

您应该在创建项时提供这些参数,或者创建另一个通用构造函数:

public item() {
   // any initialization you need here
}
public item(String ItemcodeIn, String ItemnameIn, String DescriptionIn, 
     String StyleIn, String FinishIn, float UnitpriceIn, float StockIn, 
     String SuppliercodeIn, String SuppliernameIn, String AddressIn)
public Item() {
    // Some code goes here
}

当您不编写任何构造函数时,默认构造函数在类中默认可用。但是,如果在任何类中定义并编写参数化构造函数,则只有该参数化构造函数可用。在这种情况下,您必须显式定义一个默认构造函数。

旁注:在Java中,习惯上对类名使用PascalCase,对变量名使用camelCase。因此,如果遵循约定,则类名为Item,变量名为itemCodeIn、itemNameIn、Item等。