我必须创建一个对象数组,然后随机填充。在这个数组中,我需要放置 100 个随机数,Person(base) Student(sub),Professor(sub),Course(一个 Student 和一个 Professor 的数组)和一个 Circle(不相关的)。我还必须命名并计算我进入数组的每个人(包括教授和学生)。
Object[] array = new Object[100];
String[] names = new String[]{"Ben","Anne","Joe","Sue","John","Betty","Robert","Mary",
"Mark","Jane","Paul","Willow","Alex","Courtney","Jack",
"Rachel"};
int count = 0;
for(int i=0; i<100; i++){
int a = (int)(Math.random()*5);
String n = names[(int)(Math.random()*16)];
if(a == 0){array[i]= new Person(n); count++;}
else if(a == 1){array[i]= new Student(n); count++;}
else if(a == 2){array[i]= new Professor(n); count++;}
else if(a == 3){
array[i]= new Course();
count = count + 11;
for(int j = 0; j<10; j++){
String l = names[(int)(Math.random()*16)];
array[i].getClasslist()[j].setName(l);}
}
else if(a == 4){array[i]= new Circle();}
}
但是,每当我尝试调用其中一个成员的方法时,它都会告诉我“找不到 Symbol-Method getClasslist()”或 setName 或我试图调用的任何内容。知道如何解决这个问题吗?