题解 | #定义打印方法#
定义打印方法
http://www.nowcoder.com/practice/50b50ae39d65415a9fd70601d711e5af
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());
}
}
//write your code here......
public static void print(Object obj){//Object类属于抽象类,以它作为引用可以引用任何类
System.out.println(obj.toString());//调用toString方法即可获取类名的字符串形式
}
}
class First {
public String toString() {
return "First";
}
}
class Second {
public String toString() {
return "Second";
}
}
class Third {
public String toString() {
return "Third";
}
}