Java 语言中的函数编程
2009-11-12 00:00:00 来源:WEB开发网
清单 2. 表达式特化方法package com.infosys.setl.fp;
public class SETLItem
{
private String name;
private String code;
private int price;
private String category;
public int getPrice()
{
return price;
}
public void setPrice(int inPrice)
{
price = inPrice;
}
public String getName()
{
return name;
}
public void setName(String inName)
{
name = inName;
}
public String getCode()
{
return code;
}
public void setCode(String inCode)
{
code = inCode;
}
public String getCategory()
{
return category;
}
public void setCategory(String inCategory)
{
category = inCategory;
}
}
package com.infosys.setl.fp;
import java.util.Comparator;
public class PriceComparator implements Comparator
{
public int compare (Object o1, Object o2)
{
return (((SETLItem)o1).getPrice()-((SETLItem)o2).getPrice());
}
}
package com.infosys.setl.fp;
import org.apache.commons.functor.*;
import org.apache.commons.functor.core.comparator.*;
import java.util.*;
public class TestA
{
public static void main(String[] args)
{
try
{
Comparator pc = new PriceComparator();
BinaryPredicate bp = new IsGreaterThanOrEqual(pc);
SETLItem item1 = new SETLItem();
item1.setPrice(100);
SETLItem item2 = new SETLItem();
item2.setPrice(99);
if (bp.test(item1, item2))
System.out.println("Item1 costs more than Item2!");
else
System.out.println("Item2 costs more than Item1!");
SETLItem item3 = new SETLItem();
item3.setPrice(101);
if (bp.test(item1, item3))
System.out.println("Item1 costs more than Item3!");
else
System.out.println("Item3 costs more than Item1!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
更多精彩
赞助商链接