ITPUB??¨¬3
ITPUBÂÛ̳ » ¶¯Ì¬ÓïÑÔ » ±Õ°ü£¨Closures£©ÔÚ¸÷ÖÖÓïÑÔÖеÄÀý×Ó


±êÌâ: ±Õ°ü£¨Closures£©ÔÚ¸÷ÖÖÓïÑÔÖеÄÀý×Ó
ÀëÏß 4piggy
Öм¶»áÔ±



¾«»ªÌùÊý 0
¸öÈ˿ռä 0
¼¼Êõ»ý·Ö 1074 (1646)
ÉçÇø»ý·Ö 0 (1030610)
×¢²áÈÕÆÚ 2006-7-15
ÂÛ̳»ÕÕÂ:0
      
      

·¢±íÓÚ 2006-8-5 16:19 
±Õ°ü£¨Closures£©ÔÚ¸÷ÖÖÓïÑÔÖеÄÀý×Ó

ÔÚMartin FowlerµÄһƪ¹ØÓÚ±Õ°üµÄÎÄÕÂÖУ¨http://martinfowler.com/bliki/Cl ... ¬·½±ã´ó¼ÒÔĶÁ±È½Ï¡£

1.Ruby

Õª×ÔÔ­ÎÄhttp://martinfowler.com/bliki/Closures.html


1.
def managers(emps)
  return emps.select {|e| e.isManager}
end
2.

def highPaid(emps)
  threshold = 150
  return emps.select {|e| e.salary > threshold}
end
3.

def paidMore(amount)
  return Proc.new {|e| e.salary > amount}
end¡¡
4.

highPaid = paidMore(150)
john = Employee.new
john.salary = 200
print
highPaid.call(john)
¡¡

2.c# 2.0

Õª×Ô£ºhttp://joe.truemesh.com/blog//000390.html

1.

public List<Employee> Managers(List<Employee> emps) {
  return emps.FindAll(delegate(Employee e) {
   
return e.IsManager;
  }
}
2.

public List<Employee> HighPaid(List<Employee> emps) {
  int threshold = 150;
  
return emps.FindAll(delegate(Employee e) {
    return e.Salary > threshold;
  
});
}
3.

public Predicate<Employee> PaidMore(int amount) {
  return delegate(Employee e) {
   
return e.Salary > amount;
  }
}
4.

Predicate<Employee> highPaid = PaidMore(150);
Employee john = new Employee();
john.Salary = 200;
Console.WriteLine(highPaid(john));
¡¡

3.Python

Õª×Ô£ºhttp://ivan.truemesh.com/archives/000392.html¡¡

Ö±½ÓÓÃlambdaº¯Êý

1.

def managers(emps):
    return filter(lambda e: e.isManager, emps)

2.

def highPaid(emps):
    threshold = 150
    return filter(lambda e: e.salary > threshold, emps)

3.

def paidMore(amount):
    return lambda e: e.salary > amount

4.

highPaid = paidMore(150)
john = Employee()
john.salary = 200
print highPaid(john)

ÁíÒ»ÖÖ·½Ê½£¬ÓÃÁбí°üº¬£¨list comprehensions£©

1.

def managers(emps):
    return [e for e in emps if e.isManager]

2.

def highPaid(emps):
    threshold = 150
    return [e for e in emps if e.salary > threshold]

¡¡

Chris Reedy ÌṩÁËÁíÒ»ÖÖ±ÜÃâʹÓÃlambdaµÄ·½·¨£º

def paidMore(amount):
  def paidMoreCheck(e):
    return e.salary > amount
  return paidMoreCheck


4.Java

Õª×Ô£º¡¡http://joe.truemesh.com/blog//000390.html

jdk 1.5Ö§³Ö

¡¡

private class FindHighPaidEmployees extends Algorithms {
    Constraint highPaidEmployee;
    public FindHighPaidEmployess(Constraint constraint) {
        this.highPaidEmployee = constraint;
    }
    public Object call(Object o) {
        List emps = (List)o;
        return findAll(emps, highPaidEmployee);
    }
}
private class PaidMore implements Constraint {
    private int salary;
    public PaidMore(int salary) {
        this.salary = salary;
    }
    public boolean test(Object o) {
        return ((Integer)o).intValue() > salary;
    }
}

Closure highPaidFinder = new FindHighPaidEmployees(new PaidMore(150));
List highPaidEmployees = highPaidFinder.call(myEmployees);


Ö»¿´¸Ã×÷Õß    ¶¥²¿
Ïà¹ØÄÚÈÝ


CopyRight 1999-2006 itpub.net All Right Reserved.
±±¾©ð©³½¹ãÓòÍøÂçÐÅÏ¢¼¼ÊõÓÐÏÞ¹«Ë¾. °æÈ¨ËùÓÐ
E-mail:Webmaster@itpub.net
¾©ICPÖ¤:010037ºÅ ÁªÏµÎÒÃÇ ·¨ÂɹËÎÊ