zoukankan      html  css  js  c++  java
  • Java与.net的区别delegate和event

    There is no delegate concept in Java
    The right-side C# program may be mimiced 
    with reflection technology.
    
    
    在Java中没有delegate的概念,而C#中delegate使用的是类似Java中反射的工具。
    
    
    import java.lang.reflect.*;
    import java.io.*;
    public class Test
    {
        public static void main(String[] args) throws Exception
        {
            String[] list= {"to","be","or","not","to","be"};
            Method m1 = Test.class.getMethod("toConsole",new Class[] {String.class});
            Display(m1, list);
            Method m2 = Test.class.getMethod("toFile",new Class[] {String.class});
            Display (m2, list);
        }
        public static void toConsole (String str)
        {
            System.out.print(str+" ");
        }
        public static void toFile (String s)
        {
            File f = new File("delegate.txt");
            try{
                PrintWriter fileOut = 
                   new PrintWriter(new FileOutputStream(f));
                fileOut.write(s);
                fileOut.flush();
                fileOut.close();
            }catch(IOException ioe) {}
        }
        public static void display(Method m, String[] list)
        {
            for(int k = 0; k < list.length; k++) {
                try {
                    Object[] args = {new String(list[k])};
                    m.invoke(null, args);
                }catch(Exception e) {}
            }
        }
    }
     
    
    
    Delegate是引用类型允许间接访问方法。以下是简单和多维的委托Delegate
    Delegates are reference types which allow 
    indirect calls to methods. There are single and multicast
    delegates.
    ============================================
     using System;
     using System.IO;
     public class DelegateTest
     {
         public delegate void Print (String s);
         public static void Main()
         {
             Print s = new Print (toConsole);
             Print v = new Print (toFile);
             Display (s);
             Display (v);
         }
         public static void toConsole (String str)
         {
             Console.WriteLine(str);
         }
         public static void toFile (String s)
         {
             File f = new File("delegate.txt");
             StreamWriter fileOut = f.CreateText();
             fileOut.WriteLine(s);
             fileOut.Flush();
             fileOut.Close();
         }
         public static void Display(Print pMethod)
         {
             pMethod("This should be displayed in the console");
         }
     }
     A delegate instance encapsulates one or more methods, 
     each of which is referred to as a callable entity. 
     To add or reduce a list of calls
     by using operators += or -=.
     for example
     Print p = s + v;
     s += v;
    
    
    对比可以得出的概念是:Java中没有这个Delegate的概念,但是通过反射的方法可以实现,C#却可以通过直接使用Delegate用来实现函数方法的间接调用。
  • 相关阅读:
    gulp管理静态资源缓存
    你懂AI吗(1)
    Vue.js之render函数基础
    笑看女程序员征婚SQL,半夜巡逻民警突然对我大喊int类型占几个字节
    高吞吐量的分布式发布订阅消息系统Kafka之Producer源码分析
    Java并没有衰落.大家对它的认识才刚刚开始 Java8全新出发
    那些面试官必问的JAVA多线程和并发面试题及回答
    在阿里一位新员工是怎么一步步培养起来的
    Lambda表达式用法大比较: Scala和Java 8
    国内外程序员编程网站、博客,对学编程的你提供一点小小的帮助
  • 原文地址:https://www.cnblogs.com/timlong/p/3919028.html
Copyright © 2011-2022 走看看