C# 用if语句填充只读属性?
我从学校得到一个关于练习考试的问题 我需要填写一个已经是半成品的应用程序(windows窗体应用程序) 在这一行代码中,它表示以下内容:C# 用if语句填充只读属性?,c#,if-statement,properties,C#,If Statement,Properties,我从学校得到一个关于练习考试的问题 我需要填写一个已经是半成品的应用程序(windows窗体应用程序) 在这一行代码中,它表示以下内容: public bool DangerousLoad { get { // ==> Place here the code that has been asked from assignment 1D: } } public bool Dan
public bool DangerousLoad
{
get
{
// ==> Place here the code that has been asked from assignment 1D:
}
}
public bool DangerousLoad
{
get
{
bool lpg;
bool oil;
// ==> Place here the code that has been asked from assignment 1D:
if (DangerousLoad == lpg)
{
DangerousLoad = true;
}
}
}
他们要我做的是:
作业1D(8分):
属性危险负载尚未完成。如果船舶包含危险载荷,则属性“危险载荷”的值必须为真。如果负载等于液化石油气(某种汽油)或机油,则负载是危险的。填写属性
所以我的问题是如何做到这一点?因为我尝试了以下方法:
public bool DangerousLoad
{
get
{
// ==> Place here the code that has been asked from assignment 1D:
}
}
public bool DangerousLoad
{
get
{
bool lpg;
bool oil;
// ==> Place here the code that has been asked from assignment 1D:
if (DangerousLoad == lpg)
{
DangerousLoad = true;
}
}
}
我不明白的是。。我需要将代码放在注释的正下方,但当我这样做时,它会给我一个错误,即它是一个“只读”属性
所以你。。如果危险货物中含有液化石油气或石油,我如何解决这一问题
提前谢谢
DangerousLoad = true;
这意味着您正在尝试设置自己。这是你读不懂的。您需要从“get”返回一个值
诸如此类。谢谢您的回复。我要试试这个!