The difference between Java and. NET delegate and event

Source: Internet
Author: User

There is no delegate concept in javathe right-side C # program may be mimiced with reflection technology.
There is no concept of delegate in Java, whereas in C # Delegate uses a tool similar to reflection in Java.
Import Java.lang.reflect.*;import java.io.*;p ublic 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 < l Ist.length;                k++) {try {object[] args = {new String (list[k])}; M.iNvoke (null, args);  }catch (Exception e) {}}}}
Delegate is a reference type that allows indirect access to a method. The following is a simple and multidimensional delegate delegate
Delegates is reference types which allow indirect calls to methods. There is single and multicastdelegates.============================================ 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 is displayed in the console");}     }
A delegate instance encapsulates one or more methods, each of the which are 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;
The comparison can be derived from the concept that there is no concept of this delegate in Java, but can be achieved by reflection, C # can be used directly using delegate to implement the indirect invocation of the function method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.