Java用反射实现C#的Delegate

在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});
	}
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值