【问题标题】:Constructor is undefined构造函数未定义
【发布时间】:2014-07-01 19:45:04
【问题描述】:

打包在.citydoor.imports.catalog.tools;

公共类 ProductVo {

private String product_id;
private String product_name;

public void ProductVo(String i, String n){

    product_id = i;
    product_name = n;

}


public String getProductId(){
  return this.product_id;
}

public void setProductId(String product_id){
    this.product_id = product_id;
}

public String getProductName(){
    return this.product_name;
}

public void setProductname(String product_name){
    this.product_name = product_name;
}

}

打包在.citydoor.imports.catalog.tools;

导入 java.util.ArrayList;

公共类 CatFeedBean {

ArrayList<ProductVo> parsedList = new ArrayList<ProductVo>();
ArrayList<PriceVo> priceList = new ArrayList<PriceVo>();
ArrayList<SkuVo> SkuList = new ArrayList<SkuVo>();

String[] columns = arryLines.split("/");

//String[] columns;

String productid = columns[0];
String productname = columns[1];
String skuid = columns[2];
String price = columns[3];

**ProductVo productObj = new ProductVo(productid,productname);**

//parsedList.add(productObj);
//SkuVo skuObj =  new SkuVo(skuid);
//SkuList.add(skuObj);
//PriceVo priceObj = new PriceVo(price);
//priceList.add(priceObj);

}

在粗体线处,我收到一个错误 - “构造函数 ProductVo(String, String) 未定义”。

【问题讨论】:

  • public void ProductVo 没有声明构造函数...它声明了一个名为 ProductVovoid 方法。删除void 部分。

标签: java arraylist


【解决方案1】:

这不是构造函数——它是一个返回void的方法。

public void ProductVo(String i, String n){

删除void 使其成为构造函数。您将其命名为与您的类相同,这很好,但构造函数没有声明返回类型,甚至没有声明 void

public ProductVo(String i, String n){

【讨论】:

    【解决方案2】:

    这不是构造函数:

    public void ProductVo(String i, String n){  
        product_id = i;
        product_name = n;    
    }
    

    但这是(注意没有返回类型 - 在这种情况下为void):

    public ProductVo(String i, String n){   
        product_id = i;
        product_name = n;    
    }
    

    【讨论】:

      【解决方案3】:

      您应该在定义构造函数时删除返回类型void,因为构造函数没有返回类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-11
        • 2020-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        相关资源
        最近更新 更多