C#用代理写一个Console Application

来源:百度知道 编辑:UC知道 时间:2024/06/04 18:14:09
写一个Console Application:
1)有Car, Radio, GPS类;
2)Car 有一个delegate TurnOn(bool val);
有一个Power(bool val, TurnOn it)方法;
Car并不实际包含Radio, GPS对象.
3) Radio和GPS类有Power(bool val)方法.
4) Main()中调用一个Car对象的Power()方法.
choice=Console.ReadLine();
choice=1 则把Radio的Power绑定到TurnOn上,
choice=2 则把GPS的Power绑定到TurnOn上,
choice=3 则把Radio, GPS的Power都绑定到TurnOn上

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
//Car, Radio, GPS
class Car
{
public delegate void TurnOn(bool val);
public void Power(bool val, TurnOn it)
{
it(val);
}
}
class Radio
{
public static void Power(bool val)
{
Console.WriteLine("Radio");
}
}
class GPS
{
public static void Power(bool val)
{
Console.WriteLine("GPS");
}
}

class Program
{
static void Main(string[] args)
{
string choice = Console.ReadLine();
Car car = new Car();

switch (choice)
{
case "1":
{
car.Power(true, Radio.Power);
break;