C#中的委托实现租房

来源:互联网 发布:游戏优化器哪个最好用 编辑:程序博客网 时间:2024/05/03 19:50

//House类

public  class House
    {
        public HouseType  RoomType { get; set; }
        public double Rent { get; set; }
        public House(HouseType houseType, double rent)
        {
            this.RoomType = houseType;
            this.Rent = rent;
        }
    }


// HouseType类

 public enum HouseType
    {
         OneBedroom,TwoBedroom,ThreeBedroom,FourBedroom
    }


第一步:定义委托类型

Agency类


   //1.定义委托类型
   public delegate void LetAgencyToFindHouse(HouseType houseType, double rent);
       
   public class Agency
    {
       public string Name { get; set; }
       private List<House> houselist = new List<House>();


       public Agency(string name)
       {
           this.Name = name;
           House h1 = new House(HouseType.OneBedroom, 500);
           House h2 = new House(HouseType.TwoBedroom, 700);
           House h3 = new House(HouseType.ThreeBedroom, 1000);
           House h4 = new House(HouseType.FourBedroom, 1500);
           this.houselist.Add(h1);
           this.houselist.Add(h2);
           this.houselist.Add(h3);
           this.houselist.Add(h4);
       }


       public void FindHouse(HouseType hopeType,double hopeRent)
       {
           Console.WriteLine("\n中介{0}开始找房子...",this .Name);
           House result = null;
           foreach (House  h in this.houselist)
           {
               if (h.RoomType == hopeType && h.Rent<= hopeRent)
               {
                   result = h;
               }
           }
           if (result == null)
           {
               Console.WriteLine("\n对不起!暂时没有你想要的房源!");
           }
           else 
           {
               Console.WriteLine("\n找到了你期望的房子:类型:{0},月租金为:{1}",result.RoomType,result.Rent);
           }
       }
    }


第二步:定义委托对象

Customer类


public class Customer
    {
        //2.定义委托对象
        public LetAgencyToFindHouse agencyDelegate;


        public string Name { get; set; }
        public HouseType RoomType { get; set; }
        public double Rent { get; set; }


        public void ToRentHouse()
        {
            Console.WriteLine("\n顾客{0}说,我要找一个{1}类型,月租金不高于{2}元的房子!",this.Name,this.RoomType ,this.Rent);
            // 4.调用委托
            agencyDelegate(this.RoomType, this.Rent);
        }
    }


第三步:把与委托有相同方法签名的方法绑定到对象上

程序入口Main方法


class Program
    {
        static void Main(string[] args)
        {
            Customer jayChou = new Customer();
            jayChou.Name = "周杰伦";
            jayChou.RoomType = HouseType.FourBedroom;
            jayChou.Rent = 1000;


            Agency one = new Agency("我爱我家");
            Agency two = new Agency("北大方正");
            // 3.把方法绑定到委托上
            jayChou.agencyDelegate = new LetAgencyToFindHouse(one.FindHouse);
            jayChou.agencyDelegate += new LetAgencyToFindHouse(two.FindHouse);
            jayChou.ToRentHouse();


            Console.WriteLine("\n第一家中介公司态度不好!我只要第二家帮我找...我可以出高点价格!");


            jayChou.Rent = 2000;


            jayChou.agencyDelegate -= new LetAgencyToFindHouse(one.FindHouse);
            jayChou.ToRentHouse();


            Console.ReadLine();
        }
    }

第四步:调用委托

原创粉丝点击