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.