Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4446788
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Java

2015-06-26 11:23:47

文章来源:http://blog.micro-studios.com/?p=592

1.JAVA多态接口动态加载实例

为某研究所编写一个通用程序,用来计算每一种交通工具运行 1000公里所需的时间,已知每种交通工具的参数都是3个整数A、B、C的表达式。现有两种工具:Car 和Plane,其中Car 的速度运算公式为:A*B/C,Plane 的速度运算公式为:A+B+C。需要编写三类:ComputeTime.java,Plane.java,Car007.java接口Common.java,要求在未来如果增加第3种交通工具的时候,不必修改以前的任何程序,只需要编写新的交通工具的程序。其运行过程如下,从命令行输入ComputeTime的四个参数,第一个是交通工具的类型,第二、三、四个参数分别时整数A、B、C,举例如下:      计算Plane的时间:”javaComputeTime Plane 20 30 40″
计算Car007的时间:”java ComputeTime Car007 23 34 45″
如果第3种交通工具为Ship,则只需要编写Ship.java,运行时输入:”java ComputeTime Ship 22 33 44″
提示:充分利用接口的概念,接口对象充当参数。

实例化一个对象的另外一种办法:Class.forName(str).newInstance();例如需要实例化一个Plane对象的话,则只要调用Class.forName(“Plane”).newInstance()便可。

代码:

1. ComputTime.java 请确保输入正确,其中没有捕捉NumberFromatException

import CalTime.vehicle.all.Common;
import java.lang.*;
public class ComputeTime {
public static void main(String args[]) {
System.out.println(“交通工具: “+args[0]);
System.out.println(” 参数A: “+args[1]);
System.out.println(” 参数B: “+args[2]);
System.out.println(” 参数C: “+args[3]);
double A=Double.parseDouble(args[1]);
double B=Double.parseDouble(args[2]);
double C=Double.parseDouble(args[3]);
double v,t;
try {
Common d=(Common) Class.forName(“CalTime.vehicle.”+args[0]).newInstance();
v=d.runTimer(A,B,C);
t=1000/v;
System.out.println(“平均速度: “+v+” km/h”);
System.out.println(“运行时间:”+t+” 小时”);
} catch(Exception e)    {
System.out.println(“class not found”);
}
}
}

2.Plane.java

package CalTime.vehicle;
import CalTime.vehicle.all.Common;
public class Plane implements Common {
public double runTimer(double a, double b, double c) {
return (a+ b + c);
}
}

3. Car.java

package CalTime.vehicle;
import CalTime.vehicle.all.Common;
public class Car implements Common {
public double runTimer(double a, double b, double c) {
return ( a*b/c );
}
}

4.Common.java

package CalTime.vehicle.all;
public interface Common {
double runTimer(double a, double b, double c);
}

一次运行结果:

C:\java>java     ComputeTime Car 100 45 67
交通工具: Car
参数A: 100
参数B: 45
参数C: 67
平均速度: 67.16417910447761 km/h
运行时间:14.88888888888889 小时

C:\java>java     ComputeTime Plane 130 45 67
交通工具: Plane
参数A: 130
参数B: 45
参数C: 67
平均速度: 242.0 km/h
运行时间:4.132231404958677 小时

演示了接口的经典使用方法。嗬嗬,只有意会,不可言传。 Thinking in Java 对此也做出了深刻的分析,可以查看下。

2.接口作为方法的参数传递。

先看一个接口应用的例子:

interface Extendbroadable{
public void inPut();
}
class KeyBroad     implements Extendbroadable{
public void inPut(){
System.out.println(“\n hi,keybroad has be input into then mainbroad!\n”);
}
}
class NetCardBroad     implements Extendbroadable{
public void inPut(){
System.out.println(“\n hi,netCardBroad has be input into then mainbroad!\n”);
}
}
class CheckBroad{
public void getMainMessage(Extendbroadable ext){
ext.inPut();
}
}
public class InterfaceTest01{
public     static void main(String []args){
KeyBroad kb=new KeyBroad();
NetCardBroad ncb=new NetCardBroad();
CheckBroad cb=new CheckBroad();
cb.getMainMessage(kb);
cb.getMainMessage(ncb);

}
}

可以将借口类型的参数作为方法参数,在实际是使用时可以将实现了接口的类传递给方法,后方法或按照重写的原则执行,实际调用的是实现类中的方法代码体,这样便根据传进屋的参数的不同而实现不同的功能。

重要的是,当我以后徐要林外一个对象并且拥有接受说生命的方法的时候的时候,我们不必须原类,只需新的类实现借口即可。

阅读(1250) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~