现在的位置: 首页 > 综合 > 正文

Delegate abc

2012年11月24日 ⁄ 综合 ⁄ 共 1294字 ⁄ 字号 评论关闭
namespace testAnony {
    
class SomeClass {
        
public delegate void SomeDelegate();

        public void InvokeDelegate(SomeDelegate del) {
            Console.WriteLine(
"=== BEGIN DELEGATE --------------\n");
            del();
            Console.WriteLine(
"\n=== END DELEGATE ----------------\n");
        }
    }

    public class Program {
        
static void Main(string[] args) {
            
new SomeClass().InvokeDelegate(CustomWrite);
            
new SomeClass().InvokeDelegate(AnotherCustomWrite);

            new SomeClass().InvokeDelegate(delegate() {
                Console.WriteLine(
"是的,当时就是那样");
                Console.WriteLine(
"这就是匿名函数");
            });
        }

        static public void CustomWrite() {
            Console.WriteLine(
"hello nonocast!");
        }

        static public void AnotherCustomWrite() {
            Console.WriteLine(
"good day, sir");
        }
    }
}

是的,在代码面前更多的语言都是徒劳的
It's enough..i think

BTW:
可以做一个横向关联,那就是ruby的block,原理是一样的,都属于函数式编程的范畴。

def execute
  yield
end

execute do
  puts 
"it's block!"
end

再一个大家比较熟悉的就是java中的匿名类

interface SomeInterface {
  
public void SomeMethod();
}

public class Program {
  
static public void main(String[] args) {
    
new SomeInterface() {
        
public void SomeMethod() {
          System.out.println(
"Hello World");
        }
    }.SomeMethod();
  }
}
    

抱歉!评论已关闭.