博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
源辰项目-1
阅读量:7013 次
发布时间:2019-06-28

本文共 4572 字,大约阅读时间需要 15 分钟。

接口:

==================
package com.huaiwei.ben;

public interface Filter {

public boolean doCheck( Object obj );
}
====================
package com.huaiwei.ben;

public interface Measure {

public double measure( Object obj);
}
========================
package com.huaiwei.ben;

public class Container {

public static final int Length=10000 ;
private Object[ ] objs =new Object
[Length];
private int count;
private Object max;
private Object min;
private double avg;
private double sum=0;
private Filter filter;
private Measure measure1;
public Object[] getobjs(){
Object[] newobjs=new Object
[count];
System.arraycopy( objs
,0,newobjs,0,count);
//根据count来创建一个新数组
//System.out.arraycopy();
return newobjs;
}
public void setFilter( Filter filter){
this .filter=filter;
}

public void setMeasure(Measure

measure1) {
this.measure1 = measure1;
objs= new Object[Length];
count=0;
max=null;
min=null;
avg=0;
sum=0;
}

public Object getMax() {

return max;
}

public Object getMin() {

return min;
}

public double getAvg() {

return avg;
}
public void save( Object obj){
//判断对象是否为空
if( obj==null){
return;
}
if( filter!=null){//判断接口是否
为空
if( !filter.doCheck(
obj) ){
return;
}
}
//判断当前数组是否为0,count〉=数
组。length
//TODO::将数组做成动态数组
if(count>objs.length){
System.out.println("容器
数组已满,无法操作!!");
return ;
}
//将这个obj这个数组放在数组里面
objs[count]=obj;
double
objvalue=measure1.measure(obj);
if(count ==0)
{
max=obj;
min=obj;
}else{
double
maxvalue=measure1.measure(max);
double
minvalue=measure1.measure(min);
if(objvalue>maxvalue){
max=obj;
}
if( objvalue<minvalue){
min=obj;
}
}
count++;
sum+=objvalue;
avg=sum/count;

//调用measure.measure(obj)对obj

进行d
}
}
============================
以上是对com.huaiwei.ben的操作
以下是对另一个包的操作;
===============================
package com.mimi.bean;

public class Person {

private String name;
private double height;
private double weight;
public Person(){
super();
}

public Person(String name, double

height, double weight) {
super();
this.name = name;
this.height = height;
this.weight = weight;
}

 

public String getName() {

return name;
}

public void setName(String name) {

this.name = name;
}

public double getHeight() {

return height;
}

public void setHeight(double height) {

this.height = height;
}

public double getWeight() {

return weight;
}

public void setWeight(double weight) {

this.weight = weight;
}
}
=================================
package com.mimi.bean;

import com.huaiwei.ben.Filter;

public class PersonBmiFilter implements Filter

{

@Override

public boolean doCheck(Object obj) {
if( obj == null ){
return false;
}
if( !( obj instanceof Person)){
return false;
}
Person p=(Person)obj;
if( p.getWeight()>300 &&
p.getHeight()>30){
return false;
}
return true;
}
}
==========================
package com.mimi.bean;

import com.huaiwei.ben.Measure;

public class PersonBmiTool implements Measure {

@Override

public double measure(Object obj) {
if( obj ==null){
return 0;
}
if( !(obj instanceof Person) ){
return 0;
}
Person p=(Person)obj;
double bmi=p.getWeight();
return bmi;
}

}

==============================
测试类:
import java.util.Random;

import com.huaiwei.ben.Container;

import com.mimi.bean.Person;
import com.mimi.bean.PersonBmiFilter;
import com.mimi.bean.PersonBmiTool;

public class Test {

/**

* @param args
*/
public static void main(String[] args)
{
Container c = new Container();

PersonBmiFilter fl = new

PersonBmiFilter();
PersonBmiTool pbt = new
PersonBmiTool();

c.setMeasure(pbt);

c.setFilter(fl);
Person p1 = new Person("张三",
433, 600);
Person p2 = new Person("里斯",
1.333, 63);
Person p3 = new Person("王五",
1.44, 95);

c.save(p1);

c.save(p2);
c.save(p3);

/*

* Random r=new Random(); for(
int i=0;i<100;i++){ Person p4=new
* Person("王东
风"+i,1+Math.random(), r.nextInt(80)+30);
*
* c.save(p4); }
*/
Person max = (Person) c.getMax
();
Person min = (Person) c.getMin
();

System.out.println("最大值为" +

max.getName());
System.out.println("最小值" +
min.getName());

System.out.println("平均值" +

c.getAvg());

Object[] objs = c.getobjs();

/*

* for(int i=0;i<2;i++){ Person
p11=(Person)objs[i];
* System.out.println
(p11.getName()); }
*/
for (Object o : objs) {// 增强型
的for,从objs中调用到Object o中
Person pp = (Person) o;
System.out.println
(pp.getName() + "\t" + pp.getHeight()
+ "\t"
+ pp.getWeight());

}

System.out.println

("********************************");

c.setFilter(null);

c.setMeasure(pbt);

c.save(p1);

c.save(p2);
c.save(p3);
objs=c.getobjs();

for (Object o : objs) {// 增强型

的for,从objs中调用到Object o中
Person pp = (Person) o;
System.out.println
(pp.getName() + "\t" + pp.getHeight()
+ "\t"
+ pp.getWeight());

}

}

}

转载于:https://www.cnblogs.com/yaobolove/p/4355998.html

你可能感兴趣的文章
layer.alert自定义关闭回调事件
查看>>
LESS IS MORE
查看>>
会话跟踪session
查看>>
Tablayout ViewPage 使用示例
查看>>
TP框架在做上传时候提示:没有上传的文件!
查看>>
CentOS 6.8 防火墙配置
查看>>
Java 知识点
查看>>
Nginx+Tomcat高性能负载均衡集群搭建
查看>>
BZOJ3573: [Hnoi2014]米特运输(树上乱搞)
查看>>
Dubbo的一些编码约定和设计原则
查看>>
JavaServer Faces (JSF) with Spring
查看>>
处理:“ORA-00257: archiver error. Connect internal only, until freed”的错误问题
查看>>
java 取汉字首字母
查看>>
苹果版小黄车(ofo)app主页菜单效果
查看>>
使用Genymotion模拟器或者手机运行ionic4程序
查看>>
tensorflow 在加载大型的embedding模型参数时,会遇到cannot be larger than 2GB
查看>>
SpringBoot(十八)@value、@Import、@ImportResource、@PropertySource
查看>>
SQL Server 字符串处理函数
查看>>
恢复系统管理员密码的五大奇招
查看>>
GridView 获取当前行的索引值
查看>>