采納吧
using?System;namespace?ConsoleApplication1
{
class?Program
{
static?void?Main(string[]?args)
{
var?passtrain?=?new?Passtrain(1500,"88888888",220);
Console.WriteLine(passtrain.GetMessage());//?顯示客車信息
passtrain.IsSpeeding(180);//?判斷速度是否超過180
//?輸出內容:
//?客車重量:1500?車牌號:88888888?當前速度:220
//?您已超速!
Console.ReadLine();
}
interface?IPasstrain
{
int?Weight?{?get;?set;?}
string?Plate?{?get;?set;?}
int?NowV?{?get;?set;?}
string?GetMessage();
}
public?class?Passtrain?:?IPasstrain
{
public?int?Weight?{?get;?set;?}
public?string?Plate?{?get;?set;?}
public?int?NowV?{?get;?set;?}
///?<summary>
///?當超速時觸發的事件
///?</summary>
event?Action?SpeedingEventHandler?=?()?=>?{?Console.WriteLine("您已超速!");?};
public?string?GetMessage()
{
return?$"客車重量:{Weight}?車牌號:{Plate}?當前速度:{NowV}";
}
///?<summary>
///?判斷是否超速
///?</summary>
///?<param?name="setSpeeding">限定速度</param>
public?void?IsSpeeding(int?setSpeeding)
{
if?(this.NowV?>?setSpeeding)
{
SpeedingEventHandler();
}
}
public?Passtrain(int?weight,?string?plate,?int?nowV)
{
this.Weight?=?weight;
this.Plate?=?plate;
this.NowV?=?nowV;
}
}
}
}