利用委托,模拟书店的简易运营过程,比如添加书到数据库,打印书籍作者,输出书籍的平均价格?

     

class Program
    {
        static void Main(string[] args)
        {
            Library<Book> library = new Library<Book>();
            Book book1 = new Book("斗罗大陆", "唐家三少", 21);
            Book book2 = new Book("斗破苍穹", "土豆", 22);
            Book book3 = new Book("盘龙", "番茄", 23);
            library.AddBook(book1);
            library.AddBook(book2);
            library.AddBook(book3);
            library.FindBook("斗罗大陆");
        }

    }


    class Library<T> where T : Book
    {
        //1.一般委托
        delegate int dele1();
        dele1 dele01 = new dele1(PrintPrice);
        delegate string dele2(string booker);
        dele2 dele02 = new dele2(Writer);
        //2.匿名委托
        delegate void dele3();
        dele3 dele03 = delegate { PrintPrice(); };
        delegate void dele4(string booker);
        dele4 dele04 = delegate (string booker) { Writer(booker); };
        //3.Action委托
        Action Find1 = delegate { PrintPrice(); };
        Action<string> Find2 = delegate (string booker) { Writer(booker); };
        //4.Func委托
        Func<int> func1 = PrintPrice;

        Func<string, string> func2 = Writer;


        //5.Lambda表达式委托
        delegate void Vip1();
        Vip1 vip1=()=> 
        {
            int midPrice = 0, sumPrice = 0;
            for (int i = 0; i < list.Count; i++)
            {
                sumPrice += list[i].Price;
                midPrice = sumPrice / list.Count;
            }
            Console.WriteLine("平均价:{0}", midPrice);
        };
        delegate void Vip2(string booker);
        Vip2 vip2 = (string booker) =>
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Booker == booker)
                {
                    Console.WriteLine("此书作者:{0}", list[i].Writer);
                }
            }

        };


        static List<T> list;
        public Library()
        {
            list = new List<T>();
        }
        public void AddBook(T book)
        {
            list.Add(book);
        }
        public void FindBook(string booker)
        {
            dele01();
            dele02(booker);


            dele03();
            dele04(booker);


            Find1();
            Find2(booker);


            func1();
            func2(booker);


            vip1();
            vip2(booker);

        }


        public static int PrintPrice()
        {
            int midPrice = 0, sumPrice = 0;
            for (int i = 0; i < list.Count; i++)
            {
                sumPrice += list[i].Price;
                midPrice = sumPrice / list.Count;
            }
            Console.WriteLine("平均价:{0}", midPrice);
            return midPrice;
        }
        public static string Writer(string booker)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Booker == booker)
                {
                    Console.WriteLine("此书作者:{0}", list[i].Writer);
                }
            }
            return "";
        }

    }


    class Book
    {
        string booker;
        string writer;
        int price;
        public string Booker
        {
            get { return booker; }
        }
        public string Writer
        {
            get { return writer; }
        }
        public int Price
        {
            get { return price; }
        }
        public Book() { }
        public Book(string booker, string writer, int price)
        {
            this.booker = booker;
            this.writer = writer;
            this.price = price;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值