【问题标题】:A class is telling me that it won't accept arguments, yet it's supposed to, as by its constructor一个类告诉我它不会接受参数,但它应该接受,就像它的构造函数一样
【发布时间】:2017-05-04 02:39:33
【问题描述】:

我该如何解决这个问题?当我用参数实例化一个类时,我不断收到错误消息(是的,我的构造函数中有这些参数),说该类没有参数......我将在下面包含代码和错误。这可能只是一个愚蠢的错误,但请帮助!我被困住了!

这是目标文件:

import java.util.ArrayList ;

public class Student
{
private String name , idNum , activity , date ;
private ArrayList<String> studentData = new ArrayList<String>() ;

public void Student(String nameParam , String idNumParam , String activityParam , String dateParam)
{
    studentData.add(nameParam) ;
    studentData.add(idNumParam) ;
    studentData.add(activityParam) ;
    studentData.add(dateParam) ;

    name = nameParam ;
    idNum = idNumParam ;
    activity = activityParam ;
    date = dateParam ;
}

public String getName()
{
    return studentData.get(0) ;
}

public int getIdNum()
{
    return (Integer.parseInt(studentData.get(1))) ;
}

public String lastActivity()
{
    return (studentData.get((studentData.size() - 2)) + " on " + studentData.get((studentData.size() - 1))) ;
}

public String fullHistory()
{
    String fullHistory = "" ;

    for (int i = 2 ; i < studentData.size() ; i += 2)
    {
        fullHistory += (studentData.get(i) + " on " + studentData.get((i + 1)) + "\n") ;
    }
}

public void addActivity(String activityParam , String dateParam)
{
    studentData.add(activityParam) ;
    studentData.add(dateParam) ;
}

}

这是试图实例化对象的文件:

import java.util.ArrayList ;

public class LogFileManager
{
static ArrayList<Student> studentLog = new ArrayList<Student>() ;

public static void createStudent(String nameParam , String idNumParamString , String activityParam , String dateParam)
{



    //THE ERROR OCCURS HERE:
    Student oneStudent = new Student(nameParam , idNumParamString , activityParam , dateParam) ;



    int idNumParam = Integer.parseInt(idNumParamString) ;

    if (studentLog.size() == 0)
    {
        studentLog.add(oneStudent) ;
    }
    else if (studentLog.size() == 1)
    {
        if (studentLog.get(0).getIdNum() > idNumParam)
        {
            studentLog.add(0 , oneStudent) ;
        }
        else
        {
            studentLog.add(1 , oneStudent) ;
        }
    }
    else
    {
        int currentMidInd = ((studentLog.size() - 1) / 2) ;

        while (true)
        {
            if (studentLog.get(currentMidInd).getIdNum() < idNumParam)
            {
                if ((studentLog.size() - 1) == currentMidInd)
                {
                    studentLog.add(studentLog.size() , oneStudent) ;
                    break ;
                }
                else if (studentLog.get(currentMidInd + 1).getIdNum() > idNumParam)
                {
                    studentLog.add((currentMidInd + 1) , oneStudent) ;
                    break ;
                }
                else
                {
                    currentMidInd = (currentMidInd + (studentLog.size() - 1) / 2) ;
                    continue ;
                }
            }
            else
            {
                if (currentMidInd == 0)
                {
                    studentLog.add(0 , oneStudent) ;
                    break ;
                }
                else if (studentLog.get(currentMidInd - 1).getIdNum() < idNumParam)
                {
                    studentLog.add((currentMidInd - 1) , oneStudent) ;
                    break ;
                }
                else
                {
                    currentMidInd /= 2 ;
                    continue ;
                }
            }
        }
        System.out.println("THERE WAS AN ERROR. PLEASE NOTE THAT YOUR STUDENT LOG-ADDING ALGORITHM DID NOT WORK!") ;
    }
}

public static Student findStudent(int idNum)
{
    int currentMidInd = ((studentLog.size() - 1) / 2) ;
    int lastMidInd = 0 ;

    if (studentLog.size() == 0)
    {
        return null ;
    }
    else if (studentLog.size() == 1)
    {
        if (studentLog.get(0).getIdNum() == idNum)
        {
            return studentLog.get(0) ;
        }
        else
        {
            return null ;
        }
    }
    else
    {
        while (true)
        {
            if (studentLog.get(currentMidInd).getIdNum() == idNum)
            {
                return studentLog.get(currentMidInd) ;
            }
            else if (studentLog.get(currentMidInd).getIdNum() < idNum)
            {
                if (currentMidInd == lastMidInd)
                {
                    return null ;
                }
                lastMidInd = currentMidInd ;
                currentMidInd = (((studentLog.size() - 1) + currentMidInd) / 2) ;
                continue ;
            }
            else
            {
                if (currentMidInd == lastMidInd)
                {
                    return null ;
                }
                lastMidInd = currentMidInd ;
                currentMidInd /= 2 ;
            }
        }
    }
}

}

这是回溯:

    ./LogFileManager.java:15: error: constructor Student in class Student cannot be applied to given types;
    Student oneStudent = new Student(nameParam , idNumParamString , activityParam , dateParam) ;
                         ^
    required: no arguments
    found: String,String,String,String
    reason: actual and formal argument lists differ in length
    1 error

【问题讨论】:

  • 只要去掉返回类型(void)

标签: java class object parameters


【解决方案1】:

Student 具有默认构造函数。在名为 Student 的方法之前有一个 void,请将其删除。

public Student(String nameParam , String idNumParam , 
        String activityParam , String dateParam)

【讨论】:

  • 谢谢!这让我很沮丧!
【解决方案2】:

您在构造函数上有一个void。这使它成为一个方法,并且 java 添加了默认的无参数构造函数。

删除void,你应该会很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2011-10-11
    • 2023-01-01
    相关资源
    最近更新 更多