如何访问学生类中的方法。当我打印 myList 它使用学生类中的 toString 时,我不确定如何访问其他方法。这甚至可能吗?
PS我已经在我的程序中尝试并捕获,我只是没有在此处发布以使代码更短。
public class DatabaseAccess <T> {
public T[] database;
public ArrayList<T>myList = new ArrayList<T>();
public ArrayList<T> testlist = new ArrayList<T>();
public void userInterface(){
int count = 1;
for (T i : myList){
System.out.println(count++ + ": " + i);
}
}
public void readDatabase(){
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("grocery.bin"));
database = (T[]) in.readObject();
for (int i = 0; i < database.length; i++){
myList.add(database[i]);
}
myList.add((T) "\n");
myList.add((T) "\tStudents:");
ObjectInputStream in1 = new ObjectInputStream(new FileInputStream("students.bin"));
database = (T[]) in1.readObject();
for (int i = 0; i < database.length; i++){
myList.add(database[i]);
}
}
}
public class Student implements Serializable, Comparable{
private String name, id, address;
private double gpa;
public Student(String name, String id, double gpa){
this.name = name;
this.id = id;
this.gpa = gpa;
}
public int compareTo(Object object){
Student student = (Student ) object;
if(this.gpa < student.getGpa())
return -1;
else
if(this.gpa > student.getGpa())
return 1;
else
return 0;
}
public String getName(){ return name;}
public String getId(){ return id;}
public double getGpa(){ return gpa;}
public String toString(){ return (String.format("%16s%10.4s%8s", name, id, gpa));}
}