程序:
1 using System;
2 class UserInputMonitor
3 {
4 public delegate void UserRequest(object sender,EventArgs e);
5 public event UserRequest OnUserRequest;
6 public void Run()
7 {
8 if (Console.ReadLine()=="h")
9 OnUserRequest(this,new EventArgs());
10 }
11 }
12 public class Client
13 {
14 public static void Main()
15 {
16 UserInputMonitor monitor=new UserInputMonitor();
17 new Client(monitor);
18 monitor.Run();
19 }
20 private void ShowMessage(object sender,EventArgs e)
21 {
22 Console.WriteLine("HaHa!!");
23 }
24 Client(UserInputMonitor m)
25 {
26 m.OnUserRequest+=new UserInputMonitor.UserRequest(this.ShowMessage);
27 }
28 }这是一个事件处理程序,用来监听键盘的输入.
我想把24,25,26,27行去掉,改动一下第17行,可以怎么改?
1 using System;
2 class UserInputMonitor
3 {
4 public delegate void UserRequest(object sender,EventArgs e);
5 public event UserRequest OnUserRequest;
6 public void Run()
7 {
8 if (Console.ReadLine()=="h")
9 OnUserRequest(this,new EventArgs());
10 }
11 }
12 public class Client
13 {
14 public static void Main()
15 {
16 UserInputMonitor monitor=new UserInputMonitor();
17 new Client(monitor);
18 monitor.Run();
19 }
20 private void ShowMessage(object sender,EventArgs e)
21 {
22 Console.WriteLine("HaHa!!");
23 }
24 Client(UserInputMonitor m)
25 {
26 m.OnUserRequest+=new UserInputMonitor.UserRequest(this.ShowMessage);
27 }
28 }这是一个事件处理程序,用来监听键盘的输入.
我想把24,25,26,27行去掉,改动一下第17行,可以怎么改?
1 using System;
2 class UserInputMonitor
3 {
4 public delegate void UserRequest();
5 public event UserRequest OnUserRequest;
6 public void Run()
7 {
8 if (Console.ReadLine()=="h")
9 OnUserRequest();
10 }
11 }
12 public class Client
13 {
14 public static void Main()
15 {
16 UserInputMonitor monitor=new UserInputMonitor();
17 monitor.OnUserRequest+=new UserInputMonitor.UserRequest(this.ShowMessage); 18 monitor.Run();
19 }
20 private void ShowMessage(object sender,EventArgs e)
21 {
22 Console.WriteLine("HaHa!!");
23 }
24
28 } 4 public delegate void UserRequest(); 参数如果没用的话就不用加了.
2 class UserInputMonitor
3 {
4 public delegate void UserRequest(object sender,EventArgs e);
5 public event UserRequest OnUserRequest;
6 public void Run()
7 {
8 if (Console.ReadLine()=="h")
9 OnUserRequest(this,new EventArgs());
10 }
11 }
12 public class Client
13 {
14 public static void Main()
15 {
16 UserInputMonitor monitor=new UserInputMonitor();
17 monitor.OnUserRequest+=
new UserInputMonitor.UserRequest(this.ShowMessage);
18 monitor.Run();
19 }
20 private void ShowMessage(object sender,EventArgs e)
21 {
22 Console.WriteLine("HaHa!!");
23 }
28 }
你们的方法我以前也这样改过,可是编译无法通过.
第17行:关键字“this”在静态属性、静态方法或静态字段初始值设定项中无效
不过还是谢谢你们!
monitor.OnUserRequest+=new UserInputMonitor.UserRequest(new Program().ShowMessage);
LZ要去看看基本的东西啦