packageChapter4;importjava.util.Scanner;interfaceVehicle{//父类doublegetSpeed(doublea,doubleb,doublec);}classCar007implementsVehicle{//Car007子类publicdoublegetSpeed(doub...
package Chapter4;
import java.util.Scanner;
interface Vehicle{ //父类
double getSpeed(double a,double b,double c);
}
class Car007 implements Vehicle{ //Car007子类
public double getSpeed(double a,double b,double c){
return (a*b/c);
}
}
class Plane implements Vehicle{ //Plane子类
public double getSpeed(double a,double b,double c){
return (a+b+c);
}
}
public class ComputeTime {
static double getTime(Vehicle v,double len,double a,double b,double c){ //计算移动len所花时间
return len/v.getSpeed(a, b, c);
}
public static void main(String args[]){
while(true){
Scanner sc=new Scanner(System.in); // 输入参数值
String str;
String a1,b1,c1;
double a2,b2,c2;
str=sc.next();
a1=sc.next();
b1=sc.next();
c1=sc.next();
a2=Double.parseDouble(a1);
b2=Double.parseDouble(b1);
c2=Double.parseDouble(c1);
System.out.print(str+" "+a2+" "+b2+" "+c2);
System.out.println();
double t;
try{
Vehicle v=(Vehicle) Class.forName(str).newInstance();
t=getTime(v,1000,a2,b2,c2);
System.out.println(t);
} catch(Exception e){
System.out.println("Class not Found!");
}
}
}
}
运行到最后总是Class not Found! 为什么会是找不到呢?
展开