我有这个程序,必须询问用户他们是否只想输入学生姓名或输入学生姓名和横幅ID。然后他们可以输入“ just name”或“ both”,然后将出现适当的问题。使用我制作的Student
类,我必须使用适当的构造函数将屏幕答案打印出来,无论是学生姓名还是学生姓名和横幅ID。我想我对如何创建构造函数感到困惑,说明说要创建三个构造函数,一个构造函数使用名称和横幅ID,一个构造函数仅使用名称,另一个不使用任何参数,并且要我创建它们在Student
类中,我认为它们是在main
类中创建的,以访问Student
类。
package classwork6_2;
import java.util.Scanner;
public class ClassWork6_2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Would you like to enter student's name only or name and banner ID?: ");
String response = s.nextLine();
String name;
long banID;
if(response.equalsIgnoreCase("just name")){
System.out.print("Enter student's name: ");
name = s.nextLine();
} else if(response.equalsIgnoreCase("both")){
System.out.print("Enter students name: ");
name = s.nextLine();
System.out.print("Enter student's banner ID: ");
banID = s.nextLong();
}
Student nameBanID = new Student();
nameBanID.setNameBanID(name, banID);
Student n = new Student();
n.setName(name);
System.out.print("Students name is: " + n.getName());
System.out.print("Student's banner ID is: " + n.getNameBanID());
}
}
学生班
package classwork6_2;
public class Student {
private String name;
private String bannerID;
Student nameBanID = new Student();
Student n = new Student();
Student none = new Student();
public String getNameBanID(){
return bannerID + name;
}
public String getName(){
return name;
}
public void setNameBanID(String name, long banID){
bannerID = bannerID + name;
}
public void setName(String name){
this.name = name;
}
}
答案 0 :(得分:2)
由于学生类中的这些行,您遇到了堆栈溢出错误
Student nameBanID = new Student();
Student n = new Student();
Student none = new Student();
在ClassWork6_2中,当您调用Student nameBanID = new Student();
时,您正在创建Student类的实例并将其分配给nameBanID
变量。当您创建该类的实例时,它会立即到达Student nameBanID = new Student();
行,导致您的代码经过创建新学生的循环,直到发生堆栈溢出错误为止。
这是三个构造函数的外观
private String name;
//changed bannerID to long to match input from code example
private long bannerID;
public Student(String name){
setName(name);
}
public Student(long bannerID){
setBannerID(bannerID);
}
public Student(String name, long bannerID){
setName(name);
setBannerID(bannerID);
}
您当前的代码没有定义任何构造函数,但是当您未定义构造函数时,java会为您创建一个默认构造函数。定义构造函数后,您可以使用这些构造函数创建一个Student对象。
String studentName = "Jeffery";
long bannerID = 123456789;
Student studentWithName = new Student(studentName);
Student studentWithBannerID = new Student(bannerID);
Student studentWithNameAndBannerID = new Student(studentName,bannerID);
这是我对您的学生课堂所做的所有修改
class Student {
private String name;
private long bannerID;
public Student(String name){
setName(name);
}
public Student(long bannerID){
setBannerID(bannerID);
}
public Student(String name, long bannerID){
setName(name);
setBannerID(bannerID);
}
public Student(){}
public String getNameBanID(){
return bannerID + name;
}
public String getName(){
return name;
}
public long getBannerID(){
return bannerID;
}
public void setBannerID(long bannerID){
this.bannerID = bannerID;
}
public void setName(String name){
this.name = name;
}
}
答案 1 :(得分:0)
当创建一个新的Student
对象时,然后创建并初始化另外3个Student对象。这导致学生创建的递归,从而产生java.lang.StackOverflowError
。
您似乎也没有Student
类的构造函数(例如public Student () {...}
)。理想情况下,构造函数是用于初始化类变量的地方。
尝试以下方法:不要在类本身中创建3个Student
对象,而应在main
方法中创建它们。还要为您的Student
类