《一个可扩展性较高的测量工具代码》
2014-11-05 17:08
169 查看
在写关于测量工具的这个小程序时遇到的一些注意事项和学到的一些知识希望与各位分享一下:
首先,是需求说明(由于是依次向上增长的,因此,列举的需求也是逐步递增的):
//其中比较注意的是,打包的时候不要把Test类也打包进去。(在Ubuntu里面编译的时候,不然在还要用java -d com.nh.Measure.Test命令编译)
(1)定义一个需要被测量的类,例如要测人的BMI值时定义,需要一个Person类,一个Measure接口,一个
PersonMeasure类继承Measure(测量类)。
(2)添加过滤器,添加account类,一个过滤器接口,用AccountMeasure。
(3)定义一个dataset容器,用来存放传进来的对象,再用传进来的工具对这些对象。
){ max=obj; min=obj; }else{ double maxresult=this.measure.measure( max ); double minresult=this.measure.measure( min ); if( result>maxresult){ this.max=obj; } if(result<minresult){ this.min=obj; } } count++; sum+=result; avg=sum/count; objects[count-1]=obj;
} } public void addAccount( Object obj ){
System.arraycopy(objects,0,newobjects,0,objects.length); //5个参数 原数组 起始index 数组2 起始index 拷贝长度
objects=newobjects;
}
if( this.filter!=null ){ boolean result=this.filter.accept( obj ); if(result){ count1++; objects[count1+count-1]=obj; } } }
//设置测量工具 public void setMeasure(Measure measure ){ this.measure=measure; } public int getCount1(){ return count1; } public int getCount(){ return count; } public Object getArray(int i){ return objects[i]; } public void setFilter(Filter
filter ){ this.filter=filter; } public Object getMax(){ this.max=max; return this.max; } public Object getMin(){ this.min=min; return this.min=min; } public double getAvg(){ return avg; }}
(4)增加数组判断,若已经填满,则将该数组复制给新的数组。(经同学提点,若需要用同名数组,需要定
义新的数组,不过最终还是要将新数组的引用复制给原数组。)
(5)测试类:测试两种工具;
首先,是需求说明(由于是依次向上增长的,因此,列举的需求也是逐步递增的):
//其中比较注意的是,打包的时候不要把Test类也打包进去。(在Ubuntu里面编译的时候,不然在还要用java -d com.nh.Measure.Test命令编译)
(1)定义一个需要被测量的类,例如要测人的BMI值时定义,需要一个Person类,一个Measure接口,一个
PersonMeasure类继承Measure(测量类)。
<span style="color:#FF6666;">//Person类:</span> package com.nh.Measure; //定义Person的私有属性 public class Person{ private String name; private double height; private double weight; //构造方法 public Person(String name,double height,double weight){ this.name=name; this.height=height; this.weight=weight; } public String toString(){ return "姓名"+this.name+"\t身高:"+this.height+"\t体重:"+this.weight; } public double getHeight(){ return this.height; } public double getWeight(){ return this.weight; } } <span style="color:#FF0000;">//Measure接口</span><span style="color:#C0C0C0;"> </span>package com.nh.Measure; public interface Measure{ public double measure(Object obj); } //PersonMeasure类: package com.nh.Measure; import com.nh.Measure.Measure; import com.nh.Measure.Person; public class PersonMeasure implements Measure{ public double measure(Object obj){ if(obj==null){ return 0; } if(! (obj instanceof Person) ){ return 0; } double height=((Person) obj).getHeight(); double weight=((Person) obj).getWeight(); return height*height/weight; } }
(2)添加过滤器,添加account类,一个过滤器接口,用AccountMeasure。
<span style="color:#FF0000;">//account类:</span> package com.nh.Measure; public class Account{ private int accountID; private String username; private double balance; public Account(int accountID,String username,double balance){ this.balance=balance; this.accountID=accountID; this.username=username; } public String toString(){ return "银行ID:"+this.accountID+"\t姓名:"+this.username+"\t余额:"+this.balance; } public double getBalance(){ return this.balance; } } <span style="color:#FF0000;">//Filter类:</span> package com.nh.Measure; public interface Filter{ public boolean accept( Object obj ); }
(3)定义一个dataset容器,用来存放传进来的对象,再用传进来的工具对这些对象。
<span style="color:#FF0000;">//DataSet类:</span> package com.nh.Measure; import com.nh.Measure.Measure; import com.nh.Measure.Filter; import com.nh.Measure.Account; import com.nh.Measure.Person; import com.nh.Measure.PersonMeasure; import com.nh.Measure.AccountMeasure; public class DataSet{ private Object max; //最大值BMI对象 private Object min; //最小值BMI对象 private double avg; //求均值 private double sum; private int count; private int count1; private Measure measure; private Filter filter; private Object[] objects=new Object[100]; public void addPerson( Object obj ){ if( objects[count]!=null && ( count==objects.length-1 ) ){ <pre name="code" class="java"> //由于两个测量的工具都要用该数组因此都需要对其长度进行判断Object[] newobjects=new Object[2*objects.length]; System.arraycopy(objects,0,newobjects,0,objects.length); //5个参数 原数组 起始index 数组2 起始index 拷贝长度 objects=newobjects; } if( this.measure!=null ){ double result=this.measure.measure( obj ); if( count==0
){ max=obj; min=obj; }else{ double maxresult=this.measure.measure( max ); double minresult=this.measure.measure( min ); if( result>maxresult){ this.max=obj; } if(result<minresult){ this.min=obj; } } count++; sum+=result; avg=sum/count; objects[count-1]=obj;
} } public void addAccount( Object obj ){
if( objects[count]!=null && ( count==objects.length-1 ) ){ <pre name="code" class="java"> //由于两个测量的工具都要用该数组因此都需要对其长度进行判断Object[] newobjects=new Object[2*objects.length];
System.arraycopy(objects,0,newobjects,0,objects.length); //5个参数 原数组 起始index 数组2 起始index 拷贝长度
objects=newobjects;
}
if( this.filter!=null ){ boolean result=this.filter.accept( obj ); if(result){ count1++; objects[count1+count-1]=obj; } } }
//设置测量工具 public void setMeasure(Measure measure ){ this.measure=measure; } public int getCount1(){ return count1; } public int getCount(){ return count; } public Object getArray(int i){ return objects[i]; } public void setFilter(Filter
filter ){ this.filter=filter; } public Object getMax(){ this.max=max; return this.max; } public Object getMin(){ this.min=min; return this.min=min; } public double getAvg(){ return avg; }}
(4)增加数组判断,若已经填满,则将该数组复制给新的数组。(经同学提点,若需要用同名数组,需要定
义新的数组,不过最终还是要将新数组的引用复制给原数组。)
if( objects[count]!=null && ( count==objects.length-1 ) ){ Object[] newobjects=new Object[2*objects.length]; System.arraycopy(objects,0,newobjects,0,objects.length); <span style="color:#FF0000;">//5个参数 原数组 起始index 数组2 起始index 拷贝长度 </span> objects=newobjects;<span style="color:#FF0000;"> //将新数组的引用复制给原数组;</span> }
(5)测试类:测试两种工具;
import com.nh.Measure.Measure; import com.nh.Measure.Filter; import com.nh.Measure.Account; import com.nh.Measure.Person; import com.nh.Measure.PersonMeasure; import com.nh.Measure.AccountMeasure; import com.nh.Measure.DataSet; public class Test{ public static void main(String[] args){ Person p=new Person("刘莹慧",1.62,53 ); PersonMeasure pm1=new PersonMeasure(); double bim=pm1.measure(p); System.out.printf( p.toString()+"的人的BIM值为:%1$2.2f\n",bim); //测试person dataset里的add方法 Person p1=new Person("lyh",1.62,53 ); Person p2=new Person("zt",1.72,57); Person p3=new Person("zz",1.81,65); //定义工具 PersonMeasure pm=new PersonMeasure(); //定义容器 DataSet ds=new DataSet(); //传入工具 ds.setMeasure(pm); //将对象挨个传入,存入数组内; ds.addPerson(p1); ds.addPerson(p2); ds.addPerson(p3); //将最大值的对象赋值给maxobj,这样我们就得到了最大值的对象是哪一只拉~~~~ Object maxobj=ds.getMax(); Object minobj=ds.getMin(); System.out.println( "BIN最大的是:"+maxobj.toString()+"\nBIN最小的是:"+minobj.toString() ); //测量Account dateset里的add方法 Account a1=new Account(1,"lw",334); Account a2=new Account(2,"zz",3311); Account a3=new Account(3,"az",13044); Account a4=new Account(4,"la",44); Account a5=new Account(5,"lyh",44044); //定义过滤器 AccountMeasure am=new AccountMeasure(); //定义容器()由于上面已定义过 ds.setFilter(am); //传入过滤器 ds.addAccount(a1); ds.addAccount(a2); ds.addAccount(a3); ds.addAccount(a4); ds.addAccount(a5); //首先获取数组的开始位置 int begin=ds.getCount(); int end=ds.getCount1()+ds.getCount()-1; int length=end-begin+1; int a=begin; Object[] accountarry=new Object[ length ]; //产看所有经过过滤器的账户 System.out.println("产看所有经过过滤器的账户"); for(int i=0;i<length;i++){ System.out.println("==========================="); accountarry[i]=ds.getArray(a) ; String info=((Account)accountarry[i]).toString(); System.out.println(info); a++; } } }
相关文章推荐
- Complexity:一个测量 C 代码复杂性的工具
- 一个小工具:代码行计算器!
- [辅助工具] 一个方便将ASP代码升级到ASP.NET的小工具 -- ASP Code Migrator!
- 一个工具windows du,附代码
- Linux 下的一个全新的性能测量和调式诊断工具 Systemtap,第 1 部分: kprobe
- Linux 下的一个全新的性能测量和调式诊断工具 Systemtap, 第 3 部分: Systemtap
- 自己编写的一个代码统计的小工具
- 转载一个SAP下载工具的代码,仅用于学习(来源于:http://www.dalestech.com/)
- 一个不错的代码编辑工具
- 转载一个SAP下载工具的代码,仅用于学习(来源于:http://www.dalestech.com/)
- 推荐一个代码生成工具Commonclipse
- 介绍一个VC中代码缩进的工具
- 一个不错的活动目录察看工具的代码
- 如果有一个工具可以帮助你将你的代码可视化,你需要吗?
- 转载一个SAP下载工具的代码,仅用于学习(来源于:http://www.dalestech.com/)
- 一个struts+spring+hibernate的代码生成工具
- ReSharper——一个类似eclipse 的代码工具
- FireStorm一个不错的代码生成工具
- Linux 下的一个全新的性能测量和调式诊断工具 Systemtap,第 1 部分: kprobe
- 代码生成工具应成为中国外包公司的一个应用策略