using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace FeiFei
{
public class Fun
{
public void add(int a, int b)
{
int c = a + b;
Console.WriteLine("{0}+{1}={2}", a, b, c);
}
public void sub(int a, int b)
{
int c = a - b;
Console.WriteLine("{0}-{1}={2}", a, b, c);
}
}
public class App
{
public delegate void wt(int a,int b);
public wt w;
public event wt js;
public App(int a,int b)
{
_a=a;
_b=b;
}
public void app()
{
if(w!=null)
w(_a,_b);
}
private int _a;
private int _b;
} class Program
{
static void Main(string[] args)
{
Fun f = new Fun();
App a = new App(100,50);
a.w = new App.wt(f.add);
a.w += new App.wt(f.sub);
a.js += new App.wt(f.add);
a.js();//这里调用 a.js()为什么会错误呀
//Visual Studio 2008 的错误提示
//错误1事件“FeiFei.App.js”只能出现在 += 或 -= 的左边(从类型“FeiFei.App”中使用时除外)c:\Program.cs 49 15 FeiFei
Console.ReadKey();
}
}
}
我的教程里说不能在类外调用事件,这是为什么呢?
他说要调用事件就要把事件放到一个方法里,然后要在类外调用事件就调用这个方法,
为什么要这样做啊,这个不是很麻烦?
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace FeiFei
{
public class Fun
{
public void add(int a, int b)
{
int c = a + b;
Console.WriteLine("{0}+{1}={2}", a, b, c);
}
public void sub(int a, int b)
{
int c = a - b;
Console.WriteLine("{0}-{1}={2}", a, b, c);
}
}
public class App
{
public delegate void wt(int a,int b);
public wt w;
public event wt js;
public App(int a,int b)
{
_a=a;
_b=b;
}
public void app()
{
if(w!=null)
w(_a,_b);
}
private int _a;
private int _b;
} class Program
{
static void Main(string[] args)
{
Fun f = new Fun();
App a = new App(100,50);
a.w = new App.wt(f.add);
a.w += new App.wt(f.sub);
a.js += new App.wt(f.add);
a.js();//这里调用 a.js()为什么会错误呀
//Visual Studio 2008 的错误提示
//错误1事件“FeiFei.App.js”只能出现在 += 或 -= 的左边(从类型“FeiFei.App”中使用时除外)c:\Program.cs 49 15 FeiFei
Console.ReadKey();
}
}
}
我的教程里说不能在类外调用事件,这是为什么呢?
他说要调用事件就要把事件放到一个方法里,然后要在类外调用事件就调用这个方法,
为什么要这样做啊,这个不是很麻烦?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货