Java 在对象数组的元素中调用方法
我正在编写一个简单的程序,用于存储每个学生所选的课程信息,并打印学生所选的每门课程Java 在对象数组的元素中调用方法,java,arrays,Java,Arrays,我正在编写一个简单的程序,用于存储每个学生所选的课程信息,并打印学生所选的每门课程 public class Test1Q2A { public static void main(String[] args) { Course[] listA=new Course[10]; listA[0]=new Course("EIE3320",60); listA[1]=new Course("EIE3105",40); Un
public class Test1Q2A
{
public static void main(String[] args)
{
Course[] listA=new Course[10];
listA[0]=new Course("EIE3320",60);
listA[1]=new Course("EIE3105",40);
UniversityStudent studentA=new UniversityStudent("John",2,listA);
studentA.print();
Course[] listB=new Course[10];
listB[0]=new Course("COMP1001",84);
listB[1]=new Course("EIE3105",68);
listB[2]=new Course("EIE3320",52);
UniversityStudent studentB=new UniversityStudent("Mary",3,listB);
studentB.print();
}
}
该程序由三个类组成,其中一个称为“课程”的类存储了学生参加的课程名称和学生在课程中获得的分数。类的一个对象存储一个课程信息
public class Course
{
// instance variables - replace the example below with your own
private String courseName;
private int testMarks;
private String result;
/**
* Constructor for objects of class Course
* The constructor stores the information of a course
*/
public Course(String name,int marks)
{
courseName=name;
testMarks=marks;
}
public String toString(String name,int marks) //combine the course name with the mark to make a string and return the string.
{
// put your code here
String score=Integer.toString(marks);
result=name+", "+score;
return result;
}
}
the another class is UniversityStudent that stores the information of each student namely the name of a student, number of course the student takes and the course list of the student.
public class UniversityStudent
{
// instance variables - replace the example below with your own
private String studentName;
private int courseNumber;
private Course[] list;
/**
* Constructor for objects of class UniversityStudent
*/
public UniversityStudent(String name,int number,Course[] a)
{
// initialise instance variables
studentName=name;
courseNumber=number;
list=a; //the course list of each student
}
public void print() //print out the course that each student takes.
{
// put your code here
System.out.println("Student Name:"+studentName);
for(int i=0;i<courseNumber;i++)
{
System.out.println(list[i].toString());
}
}
}
我想调用class Course中的方法toString(),以返回class Course中保存的字符串,但是当我在class University student中使用方法print()打印字符串时,我无法获得所需的结果。问题出在哪里?这是因为您实际上没有重写
toString()
方法。您的方法有一组参数,因此是一种完全不同的方法。因此,您需要将参数放在调用Course.toString(字符串名称,int标记)
方法的位置,或者需要将toString
方法更改为不带参数的方法。我建议这样做:
public String toString() {
String score = Integer.toString(testMarks);
result = courseName + ", " + score;
return result;
}
这是因为您实际上没有重写
toString()
方法。您的方法有一组参数,因此是一种完全不同的方法。因此,您需要将参数放在调用Course.toString(字符串名称,int标记)
方法的位置,或者需要将toString
方法更改为不带参数的方法。我建议这样做:
public String toString() {
String score = Integer.toString(testMarks);
result = courseName + ", " + score;
return result;
}
哦,谢谢你的提醒。我没有注意到,在调用toString()方法时,我没有在其中放入任何参数。多谢各位@不知道编程没问题。我还想指出,虽然它没有错,但您不需要使用
Integer.toString()
。如果你只做result=courseName+,“+testMarks”
你会得到同样的结果。哦,谢谢你的提醒。我没有注意到,在调用toString()方法时,我没有在其中放入任何参数。多谢各位@不知道编程没问题。我还想指出,虽然它没有错,但您不需要使用Integer.toString()
。如果你只做result=courseName+,“+testMarks”
你会得到同样的结果。