public void BoilWater() { for (int i = 0; i <= 100; i++) { temperature = i; if (temperature > 95) { BoilEvent?.Invoke(temperature); // 触发事件 } } }
public void BoilWater() { for (int i = 0; i <= 100; i++) { temperature = i; if (temperature > 95) { // 调用所有注册对象的方法 BoilEvent?.Invoke(temperature); } } }
public void BoilWater() { for (int i = 0; i < 100; i++) { temperature = i; } if (temperature >= 96) { BoildEventArgs e = new BoildEventArgs(temperature); BoilEvent?.Invoke(this, e); } }
public event BoilHandler BoilEvent; //声明事件 /// <summary> /// 烧水 /// </summary> public void BoilWater() { for (int i = 0; i <= 100; i++) { temperature = i; //if (temperature > 95) //{ // if (BoilEvent != null) // { //如果有对象注册 // BoilEvent(temperature); //调用所有注册对象的方法 // } //} BoilEvent?.Invoke(temperature); //调用所有注册对象的方法 } }