题解 | #定义打印方法#
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception//方法可能抛出异常的声明 {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String className = scanner.next();
// print就是需要你定义的方法
print(Class.forName(className).newInstance());//newInstance:弱类型,低效率,只能用于无参构造;new:强类型,高效率,能调用任何public构造;Class.forName()为获取类型的方法
}
}
//write your code here......
private static void print (Object obj){//static不能缺少
System.out.print(obj.getClass().getName());//获取类名
}
}
class First { public String toString() { return "First"; } }
class Second { public String toString() { return "Second"; } }
class Third { public String toString() { return "Third"; } }