How do I fix "error: constructor (class) in class (class) cannot be applied to given types;"
How do I fix "error: constructor (class) in class (class) cannot be applied to given types;"
所以我正在编写这段代码来创建一个使用抽象 类 或诸如此类的东西的小型数据库类型程序。我已经找到我的驱动程序代码,我正在其中创建 2 个新对象;一个带有随机参数,另一个带有第一个对象。
Student test1 = new Student("Jack", "jackjack@test.com", "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 的新手:我)
这是我的 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", "jackjack@test.com", "Math", 3.9, 10);
test1.editName("Jill");
test1.editEmail("jilljill@test.com");
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);
}
}
Bandmember
class 中没有接受学生作为参数的构造函数。构造函数当前看起来像这样:public BandMember(String name, String email, String major, double gpa, int grade)
。我了解到您可能想要创建一个带有 student
的新 Bandmember
。然后你可以在 Bandmember
class 中创建另一个构造函数。这是一个应该可以使用的模板。
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");
}
所以我正在编写这段代码来创建一个使用抽象 类 或诸如此类的东西的小型数据库类型程序。我已经找到我的驱动程序代码,我正在其中创建 2 个新对象;一个带有随机参数,另一个带有第一个对象。
Student test1 = new Student("Jack", "jackjack@test.com", "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 的新手:我)
这是我的 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", "jackjack@test.com", "Math", 3.9, 10);
test1.editName("Jill");
test1.editEmail("jilljill@test.com");
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);
}
}
Bandmember
class 中没有接受学生作为参数的构造函数。构造函数当前看起来像这样:public BandMember(String name, String email, String major, double gpa, int grade)
。我了解到您可能想要创建一个带有 student
的新 Bandmember
。然后你可以在 Bandmember
class 中创建另一个构造函数。这是一个应该可以使用的模板。
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");
}