0
public static ArrayList <Student> createStudents()
{
    ArrayList<Student> students;
    students = new ArrayList<Student>();

    students.add(new UndergradStudent("Kvin", 15, 'M', 4));
    students.add(new GraduateStudent("Kaley", 24, 'F', "Expert Systems"));
    students.add(new UndergradStudent("Mike", 25, 'M', 2));
    students.add(new GraduateStudent("Bob", 20, 'F', "Extreme Programming"));

    return students;
}
public static void printGradStudents(ArrayList<Student> students)
{
    int count;
    Student student;
    System.out.println("\n\nList of Graduate Students:\n");

    for (count=0; count<students.size(); count++)
    {
        student = students.get(count);
        if (?)
        {
            System.out.println(student);
        }
    }
}

##研究生是Student的子类,如果只想打印研究生的信息,if语句中应该放什么?##

4

3 回答 3

0
if(student instanceof (GraduateStudent))

你想检查传入的类是否是GraduateStudent 或underGraduate 的实例。它将返回一个布尔值。

于 2013-03-06T03:51:00.363 回答
0
if (student instanceof GraduateStudent)

那应该处理它。instanceof 检查它是否是该类的成员或继承自该类。所以 student 将是 GraduateStudent 和 Student 的一个实例

更新

正如评论中正确提到的那样,showInfo()向每个实例添加一个类将是另一种方法。

于 2013-03-06T03:51:16.310 回答
0

您可以使用instanceOf运算符

 if(student instanceOf GraduateStudent)
于 2013-03-06T03:51:31.503 回答