建立一个Common接口里面有compute方法,Car与Plane类继承自Common,要求用键盘输入类名,然后再调用compute方法,下面是我写的程序:
import java.util.*;
public class MAIN {
public static void main(String args[]) throws ClassNotFoundException,
IllegalAccessException, InstantiationException {
Scanner reader=new Scanner(System.in);
if(reader.hasNextLine())
{
String str=(String)reader.nextLine();
Common co=(Common) Class.forName(str).newInstance();
co.compute(12.0f,2.0f);
}
}
}
输入Car后,老说Exception in thread "main" java.lang.ClassNotFoundException: Car
是怎么回事啊
import java.util.*;
public class MAIN {
public static void main(String args[]) throws ClassNotFoundException,
IllegalAccessException, InstantiationException {
Scanner reader=new Scanner(System.in);
if(reader.hasNextLine())
{
String str=(String)reader.nextLine();
Common co=(Common) Class.forName(str).newInstance();
co.compute(12.0f,2.0f);
}
}
}
输入Car后,老说Exception in thread "main" java.lang.ClassNotFoundException: Car
是怎么回事啊
Common co=(Common) Class.forName(str).newInstance();
这句怎么来的?