【发布时间】:2018-06-13 07:22:50
【问题描述】:
在这个程序中出现了 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 {
}
【问题讨论】:
-
你调用方法,而不是
ifs。你到底想做什么? -
你想从另一个类中调用什么“if语句”?
-
我完全不明白你想做什么。请更准确地描述它。
-
sry 先生如何通过另一个类的方法调用 if 语句
-
您要调用哪个
if语句以及从哪里调用?
标签: java function if-statement methods overriding