在java中没有Delegate,但是可以通过反射实现。
我们假设有一只猫类和老鼠类。猫叫的时候老鼠就跑。但是老鼠不知道猫什么时候叫,因此我们需要一个事件去通知老鼠,告诉它们猫来了。那么这个事件我们可以用Delegate来实现。
首先,我们先针对静态方法去实现。
using System;
namespace DeletegateTest
{
class Mouse
{
private string name;
public Mouse(string name)
{
this.name = name;
}
public static void Run()
{
Console.WriteLine("Cat is coming,run.");
}
}
class Cat
{
private string name;
public Cat()
{
this.name = "Unkonw";
}
public Cat(string name)
{
this.name = name;
}
public delegate void CatShoutEventHandler();
public event CatShoutEventHandler catShout;
public void Shout()
{
Console.WriteLine("Miao,I am {0}", name);
if (catShout != null)
{
catShout();
}
}
public static void Main(string[] args)
{
Cat tom = new Cat("Tom");
tom.catShout = Mouse.Run;
tom.Shout();
}
}
}
此时,运行结果如下。我们看到,我们调用了猫叫的方法,老鼠就跑了。
下面我们用Java实现:
import java.lang.reflect.Method;
class Mouse {
private String name;
public Mouse(String name){
this.name = name;
}
public static void Run(){
System.out.println("Cat is coming,run");
}
}
public class Cat {
private String name;
public Method CatShout;
public Cat(String name){
this.name = name;
}
public void Shout(){
System.out.println("Miao,I am " + name);
if(CatShout != null){
try {
CatShout.invoke(null);
} catch (Exception e) {
System.out.println("error");
}
}
}
public static void main(String[] args) throws Exception{
Cat tom = new Cat("Tom");
tom.CatShout = Mouse.class.getMethod("Run");
tom.Shout();
}
}
结果和刚才C#的运行是一样滴。
现在我们假设有这样一个需求:我们通知老鼠是哪一只猫来了,并且通知每一只老鼠以便让大家共存亡。即就是通知老鼠猫来时需要知道猫的名字,并且将刚才的静态run方法作为实例化方法。
代码分别如下:
using System;
namespace DeletegateTest
{
class Mouse
{
private string name;
public Mouse(string name)
{
this.name = name;
}
public void Run(string catName)
{
Console.WriteLine("Cat {0} is coming,run.I am {1}", catName, name);
}
}
class Cat
{
private string name;
public Cat()
{
this.name = "Unkonw";
}
public Cat(string name)
{
this.name = name;
}
public delegate void CatShoutEventHandler(string name);
public event CatShoutEventHandler catShout;
public void Shout()
{
Console.WriteLine("Miao,I am {0}", name);
if (catShout != null)
{
catShout(name);
}
}
public static void Main(string[] args)
{
Cat tom = new Cat("Tom");
Mouse jerry = new Mouse("Jeery");
Mouse jack = new Mouse("Jack");
tom.catShout = jerry.Run;
tom.catShout += jack.Run;
tom.Shout();
}
}
}
package com.refelection.delegate;
import java.lang.reflect.Method;
class Mouse {
private String name;
public Mouse(String name){
this.name = name;
}
public void Run(String name){
System.out.println("Cat " + name + " is coming,run.I am " + this.name);
}
}
public class Cat {
private String name;
public Method CatShout;
public Cat(String name){
this.name = name;
}
public void Shout(Mouse[] mouse){
System.out.println("Miao,I am " + name);
if(CatShout != null){
try {
for(Mouse m : mouse){
CatShout.invoke(m, new Object[]{name});
}
} catch (Exception e) {
System.out.println("error");
}
}
}
public static void main(String[] args) throws Exception{
Cat tom = new Cat("Tom");
Mouse jerry = new Mouse("Jerry");
Mouse jack = new Mouse("Jack");
tom.CatShout = jerry.getClass().getMethod("Run", new Class[]{ String.class });
tom.Shout(new Mouse[] {jerry,jack});
}
}