2013-12-19 27 views
0
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()); 
     }} 

輸出:不能從主類中設置字符串的值變成二傳手

Undergarduate學生信息

名稱:約翰

基質:0192345

課程列表:空

Postgrad學生信息

名稱:山姆

基質:G015466

課程列表:[Ljava.lang.String; @ 2ac9fefa

這個問題我不能得到字符串的過程中的研究價值。我該怎麼做?我應該使用超級參考嗎?

回答

6

這裏:

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

你打印出的字符串數組返回的默認toString()。不要這樣做。遍歷數組並打印每個項目或使用java.util.Arrays.toString(...)

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

您還需要給你的課程類的有效方法toString(),一個返回courseName或許學分。此外,我會將課程領域改爲課程或courseList,以反映它不代表一門課程,而是一批課程。同樣,getter方法應該反映字段名稱的變化。