C#程序出问题了
usingSystem;namespaceeventanddelegat{classEvent{publicdelegatevoidHandle(objectx);pub...
using System;
namespace eventanddelegat
{
class Event
{
public delegate void Handle(object x);
public event Handle Click;
private int money;
public Event(int money)
{
this.money = money;
}
public int MONEY
{
get
{
return money;
}
set
{
money = value;
}
}
public void Trigger()
{
Console.WriteLine("HELLO,My Event");
if (money >= 0)
{
Click(this);
}
}
}
class Delegate
{
public void DelegateHandle(Event temp)
{
Console.WriteLine("HELLO,My Delegate");
temp.MONEY = temp.MONEY - 100;
Console.WriteLine("剩余{0}元", temp.MONEY);
}
}
class Program
{
static void Main(string[] args)
{
Event MyEvent = new Event(10000);
Delegate MyDelegate = new Delegate();
MyEvent.Click +=new Event.Handle(MyDelegate.DelegateHandle);
MyEvent.Trigger();
Console.ReadKey();
}
}
}
编译时说这句:“MyEvent.Click +=new Event.Handle(MyDelegate.DelegateHandle);
”中的DelegateHandle的重载均与委托eventanddelegate.Event.Handle不匹配 展开
namespace eventanddelegat
{
class Event
{
public delegate void Handle(object x);
public event Handle Click;
private int money;
public Event(int money)
{
this.money = money;
}
public int MONEY
{
get
{
return money;
}
set
{
money = value;
}
}
public void Trigger()
{
Console.WriteLine("HELLO,My Event");
if (money >= 0)
{
Click(this);
}
}
}
class Delegate
{
public void DelegateHandle(Event temp)
{
Console.WriteLine("HELLO,My Delegate");
temp.MONEY = temp.MONEY - 100;
Console.WriteLine("剩余{0}元", temp.MONEY);
}
}
class Program
{
static void Main(string[] args)
{
Event MyEvent = new Event(10000);
Delegate MyDelegate = new Delegate();
MyEvent.Click +=new Event.Handle(MyDelegate.DelegateHandle);
MyEvent.Trigger();
Console.ReadKey();
}
}
}
编译时说这句:“MyEvent.Click +=new Event.Handle(MyDelegate.DelegateHandle);
”中的DelegateHandle的重载均与委托eventanddelegate.Event.Handle不匹配 展开
2个回答
展开全部
public void DelegateHandle(object t)
{
Event temp = t as Event;
Console.WriteLine("HELLO,My Delegate");
temp.MONEY = temp.MONEY - 100;
Console.WriteLine("剩余{0}元", temp.MONEY);
}
把中间Delegate类得方法参数改一下,因为您在 public delegate void Handle(object x);
中定义的Handle的委托类型的形参是object类型,参数要与委托类型严格一致,你可以到事件处理的函数里再把这个参数转化为你要用的Event类型,当然,最好加一个类型验证。
特意给你把代码复制到vs里,这个地方改了就肯定能运行了。
{
Event temp = t as Event;
Console.WriteLine("HELLO,My Delegate");
temp.MONEY = temp.MONEY - 100;
Console.WriteLine("剩余{0}元", temp.MONEY);
}
把中间Delegate类得方法参数改一下,因为您在 public delegate void Handle(object x);
中定义的Handle的委托类型的形参是object类型,参数要与委托类型严格一致,你可以到事件处理的函数里再把这个参数转化为你要用的Event类型,当然,最好加一个类型验证。
特意给你把代码复制到vs里,这个地方改了就肯定能运行了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询