展开全部
NewInterface.javapackage baidu.itfc;
public interface NewInterface {
public abstract double getTime(double u);
}
People.javapackage baidu.itfc;
public abstract class People implements NewInterface {
// 姓名
protected String name;
// 身高
protected int height;
// 体重
protected int weight;
// 力量
protected int power;
@Override
public double getTime(double u) {
// 计算时间
double d = (height + weight + power)/u;
return d;
}
}
Time.javapackage baidu.itfc;
public class Time {
public static void TimtOut(NewInterface iPeople){
System.out.println("完成工作需要时间:" + iPeople.getTime(79.58));
}
}
ZhangSan.javapackage baidu.itfc;
public class ZhangSan extends People{
/**
* 构造方法
*/
public ZhangSan(){
name = "张三";
height = 170;
weight = 130;
power = 50;
}
}
LiSi.javapackage baidu.itfc;
public class LiSi extends People {
public LiSi(){
name = "李四";
height = 165;
weight = 102;
power = 47;
}
}
测试类62616964757a686964616fe78988e69d8331333332643264:TimeTest.javapackage baidu.itfc;
public class TimeTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
// 所在的包名路径
String path = "baidu.itfc.";
// 构建接口对象
NewInterface iPeople = (NewInterface) Class.forName(path + "WangWu").newInstance();
Time.TimtOut(iPeople);
iPeople = (NewInterface) Class.forName(path + "ZhangSan").newInstance();
Time.TimtOut(iPeople);
iPeople = (NewInterface) Class.forName(path + "LiSi").newInstance();
Time.TimtOut(iPeople);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}