我如何修复“错误:constructor(class)类(类)中的构造函数(类)不能应用于给定的类型;
因此,我正在编写此代码来创建使用抽象类或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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
bandmember
类中没有构造函数接受学生作为参数。构造函数当前如下所示:public bandmember(字符串名称,字符串电子邮件,字符串专业,双GPA,int等级)
。我了解您可能想用student
创建一个新的bandmember
。然后,您可以在bandmember
类中创建另一个构造函数。这是一个应该起作用的模板。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 newBandmember
with astudent
. Then you can create another constructor inside theBandmember
class. Here is a template that should work.