Plane类 ABC3个属性 还有一个 速度=A+B+C
Car007类 ABC3属性 速度=A*B/C
Common接口我觉得没必要如果有必要的话 那就get A B C 吧。。。
而那些工具类只用写具体的算法就行了。。。
本人菜鸟。。。个人意见。。。不要误人。。。
==================Common.java==================================package baidu
public interface Common {
int DISTANCE = 1000
float computeTime(int a,int b,int c)
}
========================Plane.java===============================
package baidu
public class Plane implements Common {
public float computeTime(int a, int b, int c) {
// TODO Auto-generated method stub
float speed = a + b + c
return DISTANCE/speed
}
}
===========================Car007.java=============================
package baidu
public class Car007 implements Common {
public float computeTime(int a, int b, int c) {
// TODO Auto-generated method stub
float speed = a*b/(float)c
return DISTANCE/speed
}
}
============================ComputeTime.java========================
package baidu
public class ComputeTime {
/**
* @param args
*/
static String packageName = "baidu"
static String className
static int a,b,c
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
className = args[0]
a = Integer.parseInt(args[1])
b = Integer.parseInt(args[2])
c = Integer.parseInt(args[3])
}catch(Exception e){
System.out.println("请输入正确的参数:className a b c")
System.exit(0)
}
Common common = null
try {
common = (Common) Class.forName(packageName + "." + className).newInstance()
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("请检查输入的className")
e.printStackTrace()
System.exit(0)
}
System.out.println(className + "所需要的时间是:" + common.computeTime(a, b, c))
}
}
//ComputeTime.javapublic class ComputeTime {
public static void main(String[] args) throws Exception{
for(int i=0i<args.lengthi++){
System.out.println(i+"-----"+args[i])
}
if(args.length!=4){
System.out.println("参数个数不对,请重新输入!")
return
}
Common com =(Common)Class.forName(args[0]).newInstance()
int a = Integer.parseInt(args[1])
int b = Integer.parseInt(args[2])
int c = Integer.parseInt(args[3])
double[] kk = com.getTime(a, b, c)
System.out.println(args[0]+"的速度为"+kk[0]+"公里/小时,走1000公里需要"+kk[1]+"小时")
}
}
//Common.java
public interface Common {
public double[] getTime(int A,int B,int C)
}
//Ship.java
public class Ship implements Common{
public double[] getTime(int A, int B, int C) {
double suDu = A*B*C
double shiJian = 1000/suDu
return new double[]{suDu,shiJian}
}
}
//Plane.java
public class Plane implements Common{
public double[] getTime(int A, int B, int C) {
double suDu = A+B+C
double shiJian = 1000/suDu
return new double[]{suDu,shiJian}
}
}
//..........交通工具类
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)