C#_事件学习

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

namespace Lab7_4
{
    class Stock
    {
        public delegate void PriceRise(double money);     //定义委托
        public delegate void PriceDown(double money);

        public event PriceRise PriceRiseEvent;            //定义事件
        public event PriceDown PriceDownEvent;

        private string stockName;
        private double stockPrice;

        public string StockName {
            set;
            get;
        }

        public double StockPrice {
            set;
            get;
        }

        public Stock(string name, double price) {
            stockName = name; stockPrice = price;
        }

        public void priceRise(double up) {
            Console.WriteLine("股票上升: {0}", up);
            PriceRiseEvent(up);             //触发事件

        }

        public void priceDown(double down) {
            Console.WriteLine("股票下降: {0}", down);
            PriceDownEvent(down);
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab7_4
{
    class Buyer
    {
        private Stock stock;           //卖家要抛售的股票

        public Buyer(Stock s) {
            stock = s;
        }

        public void StockPriceRise(double up) {

            if (up > 0.05) {
                Console.WriteLine("股票上涨已经超过5%, 买家谨慎买入");
            } else {
                Console.WriteLine("股票正在小幅上涨, 买家可以买入");
            }
        }

        public void StockPriceDown(double down) {
            if (down > 0.05) {
                Console.WriteLine("股票下跌已经超过0.05,买家可以逢低买入");
            } else {
                Console.WriteLine("股票正在小幅下跌,买家暂时请观望");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab7_4
{
    class Seller
    {
        private Stock stock;           //卖家要抛售的股票

        public Seller(Stock s) {
            stock = s;
        }

        public void StockPriceRise(double up) {

            if (up > 0.05) {
                Console.WriteLine("股票上涨已经超过5%, 卖家请赶快抛售");
            } else {
                Console.WriteLine("股票正在小幅上涨, 卖家可以继续观望");
            }
        }

        public void StockPriceDown(double down) {
            if (down > 0.05) {
                Console.WriteLine("股票下跌已经超过0.05,卖家请逢低补仓");
            } else {
                Console.WriteLine("股票正在小幅下跌,卖家请谨慎操作");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab7_4
{
    class Program
    {
        static void Main(string[] args) {

            Console.WriteLine("输入股票名: ");
            Stock stock = new Stock(Console.ReadLine(), 30000);

            Buyer buyer = new Buyer(stock);
            Seller seller = new Seller(stock);

            //注册事件
            stock.PriceRiseEvent += buyer.StockPriceRise;     
            stock.PriceDownEvent += buyer.StockPriceDown;

            stock.PriceRiseEvent += seller.StockPriceRise;
            stock.PriceDownEvent += seller.StockPriceDown;

            //触发事件
            Console.WriteLine("输入涨率: ");
            stock.priceRise(double.Parse(Console.ReadLine()));
            Console.WriteLine("输入降率: ");
            stock.priceDown(double.Parse(Console.ReadLine()));

        }
    }
}

 

posted @ 2017-05-03 09:56  douzujun  阅读(308)  评论(0编辑  收藏  举报