您的位置:首页 > 编程语言

《一个可扩展性较高的测量工具代码》

2014-11-05 17:08 169 查看
在写关于测量工具的这个小程序时遇到的一些注意事项和学到的一些知识希望与各位分享一下:

首先,是需求说明(由于是依次向上增长的,因此,列举的需求也是逐步递增的):

//其中比较注意的是,打包的时候不要把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++;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息