我的构造函数出错?

时间:2018-05-20 12:44:31

标签: java swing

第42行我有错误(新的Chanteur ......) Chanteur类中的构造函数Chanteur不能应用于给定类型;   required:int,String,String,String,Date   found:int,String

原因:实际和正式的参数列表长度不同

(Alt-Enter显示提示)

public ArrayList <Album> selectAlbums ()
    {
        ArrayList <Album> myList = new ArrayList();

        String req = "SELECT album.CodeA,album.TitreA,chanteur.IdentC,album.SortieA AS IdentC, chanteur.NomC FROM album INNER JOIN chanteur ON (album.IdentC=chanteur.IdentC) ORDER BY CodeA ASC ";

        ResultSet resu = ConnexionMySQL.getInstance().selectQuery (req);

        try {
            while (resu.next())
            {
                myList.add (new Album(resu.getString("CodeA"), resu.getString("TitreA"), 
                             new Chanteur (resu.getInt("IdentC"),resu.getString("NomC")), 
                                  resu.getDate("SortieA")));


            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAlbumsMySQL.class.getName()).log(Level.SEVERE, null, ex);
        }

        return myList;
    }

很明显,我的问题是在我的Class Chanteur中使用构造函数? 我没有看到问题?我不明白为什么我的代码失败了......

3 个答案:

答案 0 :(得分:0)

在构造函数中,您正在请求int,String,String,String和Date。在您的代码中,您只提供int,String和Date。

如果您只想提供3个参数,则可以创建另一个只需要3个参数的构造函数。研究重载。

答案 1 :(得分:0)

Chanteur的构造函数期望:

int identC, String nomC, String prenomC, String sexeC, Date NaissC

传递给构造函数的参数是:

resu.getInt("IdentC"), resu.getString("NomC")), resu.getDate("SortieA"))

它只是不匹配。

您可以创建一个接受int, String, Date的新构造函数:

public Chanteur(int identC, String nomC, Date NaissC) {
    this.identC = identC;
    this.nomC = nomC;
    this.NaissC = NaissC;
}

或者您需要将完整参数传递给当前参数。

答案 2 :(得分:0)

构造函数重载是指向同一个类提供两个或多个构造函数但使用不同参数的地方,这使您的类能够在运行时根据传递给它的值来决定调用哪个构造函数。

对于您的情况,请考虑将此构造函数添加到您的类中,该类将重载当前构造函数并在执行代码时调用

public Chanteur(int identC, String nomC, Date NaissC) {
this.identC = identC;
this.nomC = nomC;
this.NaissC = NaissC;
}