我如何修复“错误:constructor(class)类(类)中的构造函数(类)不能应用于给定的类型;

发布于 2025-01-23 18:47:56 字数 4507 浏览 0 评论 0原文

因此,我正在编写此代码来创建使用抽象类或Whatot的迷你数据库类型程序。我已经进入了我的驱动程序代码,在那里我创建了2个新对象;一个带有随机参数,另一个带有第一个对象。

Student test1 = new Student("Jack", "[email protected]", "Math", 3.9, 10);
BandMember test2 = new BandMember(test1);

但是,当我运行程序时,会发生此错误:

CollegeDatabase.java:122: error: constructor BandMember in class BandMember cannot be applied to given types;
    BandMember test2 = new BandMember(test1);
                       ^
required: String,String,String,double,int
found: Student
  reason: actual and formal argument lists differ in length
1 error

我真的很困惑此错误。如果有人可以帮助我解决这个问题,我将非常感谢。谢谢你!

(请记住,我目前在Java的新手:i)

这是我的collegeatabase.java:

abstract class Person{
    public String emailAddress, personName;
    
    public Person(String name, String email){
        System.out.println("created person");
        name = personName;
        email = emailAddress;
    }
    
    public String returnName(){
        return personName;
    }
    
    public String returnEmail(){
        return emailAddress;
    }
    
    public void editName(String newName){
        personName = newName;
        System.out.println("successfully changed name");
    }
    
    public void editEmail(String newEmail){
        emailAddress = newEmail;
        System.out.println("successfully changed email");
    }
}

class Student extends Person{
    public String majorSubject;
    public double gradePointAverage;
    public int gradeLevel;
    
    public Student(String name, String email, String major, double gpa, int grade){
        super(name, email);
        major = majorSubject;
        gpa = gradePointAverage;
        grade = gradeLevel;
    }
    
    public void changeGPA(double newGPA){
        gradePointAverage = newGPA;
        System.out.println("changed gpa");
    }
    
    public void changeMajor(String newMajor){
        majorSubject = newMajor;
        System.out.println("changed major");
    }
    
    public void changeGrade(int newGrade){
        gradeLevel = newGrade;
        System.out.println("changed grade");
    } 
    
    
    public double getGPA(){
        return gradePointAverage;
    }
    
    public String getMajor(){
        return majorSubject;
    }
    
    public int getGrade(){
        return gradeLevel;
    }
    public String toString(){
        return "Name: "+personName+", Email: "+emailAddress+", Major: "+majorSubject+", GPA: "+gradePointAverage+", Grade: "+gradeLevel+"th";
    }
}

class BandMember{
    private String name, email, major, instrument;
    private double gpa;
    private int grade;
    
    public BandMember(String name, String email, String major, double gpa, int grade){
        this.name = name;
        this.email = email;
        this.major = major;
        this.gpa = gpa;
        this.grade = grade;
        System.out.println("created new band member");
    }
    public void assignInstruments(){
        if(grade == 9){
            instrument = "bells";
        }
        else if(grade == 10){
            instrument = "trumpet";
        }
        else if(grade == 11){
            instrument = "baritone";
        }
        else if (grade == 12){
            instrument = "tuba";
        }
    }
    public void changeInstrument(String instrument){
        this.instrument = instrument;
        System.out.println("successfuly changed instruments");
    }
    
    public String toString(){
        return "Name: "+name+", Email: "+email+", Major: "+major+", GPA: "+gpa+", Grade: "+grade+"th, Instrument: "+instrument;
    }
}

public class CollegeDatabase{
    
    public static void main(String[] args)
    {
        Student test1 = new Student("Jack", "[email protected]", "Math", 3.9, 10);
        
        test1.editName("Jill");
        test1.editEmail("[email protected]");
        test1.changeGPA(4.0);
        test1.changeMajor("computer science");
        test1.changeGrade(11);
        
        BandMember test2 = new BandMember(test1);
        //error is above ^
        test2.assignInstruments();

        System.out.println(test1);
        System.out.println(test2);
    }
}

So I'm writing this a piece of code to create a mini-database type program that uses abstract classes or whatnot. I've gotten to my driver code, where I'm creating 2 new objects; one with random parameters, and the other with the first object.

Student test1 = new Student("Jack", "[email protected]", "Math", 3.9, 10);
BandMember test2 = new BandMember(test1);

But when I run my program, this error occurs:

CollegeDatabase.java:122: error: constructor BandMember in class BandMember cannot be applied to given types;
    BandMember test2 = new BandMember(test1);
                       ^
required: String,String,String,double,int
found: Student
  reason: actual and formal argument lists differ in length
1 error

I'm really confused as to why this error is occurring. I would greatly appreciate it if someone could help me resolve this issue. Thank you!

(keep in mind I'm a novice at java currently :I)

Here's my CollegeDatabase.java:

abstract class Person{
    public String emailAddress, personName;
    
    public Person(String name, String email){
        System.out.println("created person");
        name = personName;
        email = emailAddress;
    }
    
    public String returnName(){
        return personName;
    }
    
    public String returnEmail(){
        return emailAddress;
    }
    
    public void editName(String newName){
        personName = newName;
        System.out.println("successfully changed name");
    }
    
    public void editEmail(String newEmail){
        emailAddress = newEmail;
        System.out.println("successfully changed email");
    }
}

class Student extends Person{
    public String majorSubject;
    public double gradePointAverage;
    public int gradeLevel;
    
    public Student(String name, String email, String major, double gpa, int grade){
        super(name, email);
        major = majorSubject;
        gpa = gradePointAverage;
        grade = gradeLevel;
    }
    
    public void changeGPA(double newGPA){
        gradePointAverage = newGPA;
        System.out.println("changed gpa");
    }
    
    public void changeMajor(String newMajor){
        majorSubject = newMajor;
        System.out.println("changed major");
    }
    
    public void changeGrade(int newGrade){
        gradeLevel = newGrade;
        System.out.println("changed grade");
    } 
    
    
    public double getGPA(){
        return gradePointAverage;
    }
    
    public String getMajor(){
        return majorSubject;
    }
    
    public int getGrade(){
        return gradeLevel;
    }
    public String toString(){
        return "Name: "+personName+", Email: "+emailAddress+", Major: "+majorSubject+", GPA: "+gradePointAverage+", Grade: "+gradeLevel+"th";
    }
}

class BandMember{
    private String name, email, major, instrument;
    private double gpa;
    private int grade;
    
    public BandMember(String name, String email, String major, double gpa, int grade){
        this.name = name;
        this.email = email;
        this.major = major;
        this.gpa = gpa;
        this.grade = grade;
        System.out.println("created new band member");
    }
    public void assignInstruments(){
        if(grade == 9){
            instrument = "bells";
        }
        else if(grade == 10){
            instrument = "trumpet";
        }
        else if(grade == 11){
            instrument = "baritone";
        }
        else if (grade == 12){
            instrument = "tuba";
        }
    }
    public void changeInstrument(String instrument){
        this.instrument = instrument;
        System.out.println("successfuly changed instruments");
    }
    
    public String toString(){
        return "Name: "+name+", Email: "+email+", Major: "+major+", GPA: "+gpa+", Grade: "+grade+"th, Instrument: "+instrument;
    }
}

public class CollegeDatabase{
    
    public static void main(String[] args)
    {
        Student test1 = new Student("Jack", "[email protected]", "Math", 3.9, 10);
        
        test1.editName("Jill");
        test1.editEmail("[email protected]");
        test1.changeGPA(4.0);
        test1.changeMajor("computer science");
        test1.changeGrade(11);
        
        BandMember test2 = new BandMember(test1);
        //error is above ^
        test2.assignInstruments();

        System.out.println(test1);
        System.out.println(test2);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘落散花 2025-01-30 18:47:56

bandmember类中没有构造函数接受学生作为参数。构造函数当前如下所示:public bandmember(字符串名称,字符串电子邮件,字符串专业,双GPA,int等级)。我了解您可能想用student创建一个新的bandmember。然后,您可以在bandmember类中创建另一个构造函数。这是一个应该起作用的模板。

public BandMember(Student student){
    this.name = student.returnName();
    this.email = student.returnEmail();
    this.major = student.getMajor();
    this.gpa = student.getGPA();
    this.grade = student.getGrade();
    System.out.println("created new band member");
}

There is no constructor in Bandmember class that accepts a student as an argument. The constructor currently looks like this: public BandMember(String name, String email, String major, double gpa, int grade). I understand that you might want to create a new Bandmember with a student. Then you can create another constructor inside the Bandmember class. Here is a template that should work.

public BandMember(Student student){
    this.name = student.returnName();
    this.email = student.returnEmail();
    this.major = student.getMajor();
    this.gpa = student.getGPA();
    this.grade = student.getGrade();
    System.out.println("created new band member");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文