gpt4 book ai didi

java - 无法将主类中的字符串值设置为setter

转载 作者:行者123 更新时间:2023-12-01 08:07:16 26 4
gpt4 key购买 nike

public class StudentTest{

public static void main(String args[]){

UnderGrad uG1 = new UnderGrad();
uG1.setName("John");
uG1.setMatric("0192345");

System.out.println("Undergarduate Student Info");
uG1.setCourse(new Course("CS1103",3));
uG1.setCourse(new Course("IT4504",3));
uG1.displayStudentInfo();

System.out.println(" ");

PostGrad pG1 = new PostGrad();
pG1.setName("Sam");
pG1.setMatric("G015466");
pG1.setResearch("Empirical Software Engineering");
pG1.setResearch("Data Mining");
System.out.println("Postgrad Student Info");
pG1.displayStudentInfo();
}
}

public class Course{
private String courseName;
private int crhour;

public Course(String n, int c){
courseName = n;
crhour = c;
}

public void setCourseName(String course){
courseName = course;
}

public String getCourseName(){
return courseName;
}

public void setCreditH(int c){
crhour = c;
}
}

public class Student{

private String matric ="-matric required-";
private String name="-name required-";

public Student(){
}

public void setName(String n){
if (n.matches("[a-zA-Z]+") == false)
System.out.println("Invalid Name");
else
name = n;
}
public String getName(){
return name;}


public void setMatric(String m){
matric = m;}
public String getMatric(){
return matric;}
}

public class UnderGrad extends Student{
private Course courseList[];
private int index = 0;

public UnderGrad(){
Course courseList[] =new Course[7];}

public void setCourse(Course courseName){
//Course courseList[]= new Course[2];
}

public Course[] getCourse(){
return courseList;}

public void displayStudentInfo(){

System.out.println("Name: "+getName());
System.out.println("Matric: "+getMatric());
System.out.println("Course List: "+getCourse());
}}

public class PostGrad extends Student{
private String researchArea[];
private int index = 0;

public PostGrad()
{
researchArea = new String[5];
}

public void setResearch(String research){
for(index=0;index<2;index++){
researchArea[index]=research;}
}

public String[] getResearch(){
return researchArea;}

public void displayStudentInfo(){

System.out.println("Name: "+getName());
System.out.println("Matric: "+getMatric());
System.out.println("Research List: "+getResearch());
}}

输出:

本科生信息

姓名:约翰

矩阵:0192345

类(class)列表:空

研究生信息

姓名:山姆

矩阵:G015466

类(class)列表:[Ljava.lang.String;@2ac9fefa

问题是我无法获取类(class)和研究的 String 值。我该怎么办?我应该使用 super 引用吗?

最佳答案

这里:

System.out.println("Course List: "+getCourse());

您正在打印字符串数组返回的默认toString()。不要那样做。遍历数组并打印每个项目,或者使用 java.util.Arrays.toString(...)。

System.out.println("Course List: "+ java.util.Arrays.toString(getCourse()));

您还需要为您的 Course 类提供一个有效的 toString() 方法,该方法返回 courseName 以及可能的学分。另外,我会将类(class)字段更改为类(class)或类(class)列表,以反射(reflect)它并不代表一门类(class),而是代表一组类(class)。同样,getter 方法应该反射(reflect)字段名称的更改。

关于java - 无法将主类中的字符串值设置为setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20692900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com