WEB开发网
开发学院软件开发Java Java 语言中的函数编程 阅读

Java 语言中的函数编程

 2009-11-12 00:00:00 来源:WEB开发网   
核心提示: 清单 2. 表达式特化方法packagecom.infosys.setl.fp;publicclassSETLItem{privateStringname;privateStringcode;privateintprice;privateStringcategory;publicintgetPr


清单 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(); 
 } 
 } 
} 

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:Java 函数

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接