1. Trang chủ
  2. » Giáo Dục - Đào Tạo

C lab06

4 43 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

PROGRAMMING IN C# Module 12: Events and Delegates Lab Guide for Lab6 Session Objectives In this session, you will be practicing with  Create and fire an event  Subcribing to event Part – Getting started (30 minutes) Creating a delegate and subscribe some methods to it Declare delegate Incremen: void Increment(ref int X); Define methods that increment a variable to and 3, subcribe these methods to a declared delegate and invoke these methods through the delegate Scan the code first, type the code, compile, run and observe the result Create class Program class Program { public void Increment(ref int X); public static void Add2(ref int x) { x += 2; } public static void Add3(ref int x) { x += 3; } public static void Main() { Increment functionDelegate = Add2; functionDelegate += Add3; functionDelegate += Add2; int x = 5; functionDelegate(ref x); Console.ReadLine(); } } Create an application that simulate some devices like air conditioner must something when the environment’s temperature changes So create class Thermostat that monitor environment’s temperature It has an event where other devices will subcribe to it Scan the code first, type the code, compile, run and observe the result Class Thermostat class Thermostat { public delegate void TemperatureChangeHandler( float newTemperature); // Define the event publisher private TemperatureChangeHandler _OnTemperatureChange; public TemperatureChangeHandler OnTemperatureChange { get { return _OnTemperatureChange; } set { _OnTemperatureChange = value; } } public float CurrentTemperature { get { return _CurrentTemperature; } set { if (value != CurrentTemperature) { _CurrentTemperature = value; // Call subscribers if (OnTemperatureChange!=null) { OnTemperatureChange(value); } } } } private float _CurrentTemperature; } Class Cooler class Cooler { public Cooler(float temperature) { Temperature = temperature; } public float Temperature { get { return _Temperature; } set { _Temperature = value; } } private float _Temperature; public void OnTemperatureChanged(float newTemperature) { if (newTemperature > Temperature) { System.Console.WriteLine("Cooler: On"); } else { System.Console.WriteLine("Cooler: Off"); } } } Class Heater class Heater { public Heater(float temperature) { Temperature = temperature; } public float Temperature { get { return _Temperature; } set { _Temperature = value; } } private float _Temperature; public void OnTemperatureChanged(float newTemperature) { if (newTemperature < Temperature) { System.Console.WriteLine("Heater: On"); } else { System.Console.WriteLine("Heater: Off"); } } } Class Program class Program { static void Main(string[] args) { Thermostat thermostat = new Thermostat(); Heater heater = new Heater(60); thermostat.OnTemperatureChange += heater.OnTemperatureChanged; Heater heaterCuong = new Heater(100); thermostat.OnTemperatureChange += heaterCuong.OnTemperatureChanged; Cooler cooler = new Cooler(80); thermostat.OnTemperatureChange += cooler.OnTemperatureChanged; string temperature; Console.Write("Enter temperature: "); temperature = Console.ReadLine(); thermostat.CurrentTemperature = int.Parse(temperature); Console.ReadLine(); } } Part – Workshops (30 minutes) • • Quickly look at workshops of Module 12 Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Part – Lab Assignment (60 minutes) Do the assignment for Module 12 carefully Discuss with your class-mates and your instructor if needed Part – Do it your self

Ngày đăng: 27/10/2019, 10:09

Xem thêm:

Mục lục

    Part 1 – Getting started (30 minutes)

    Part 3 – Lab Assignment (60 minutes)

    Part 4 – Do it your self

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w