在这个程序中,if 语句必须从另一个类名为 Studentfactory 的类中调用。
public class Control {
public static void main(String[] args) {
Student[] studs = new Student[3];
for (int i = 0; i < 3; i++) {
studs[i] = createStudent();
}
for (int i = 0; i < 3; i++) {
System.out.println(studs[i]);
}
}
static Student createStudent() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name:");
String name = sc.nextLine();
System.out.print("Enter your age:");
int age = sc.nextInt();
if(age <20) {
return new JuniorStudent(name, age);
} else if( age < 30) {
return new IntermediateStudent(name,age);
}
return new SeniorStudent(name, age);
}
}
// if 语句必须从这个类调用
package demo;
public class Studentfactory {
}
如果我理解正确,您想从类中调用createStudent
类的静态方法Control
StudentFactory
这样做的方法是
Control.createStudent()
(在课堂上StudentFactory
)
希望这可以帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句