forname java_JAVA中 class.forName()的应用

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! 为什么会是找不到呢?

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值