创建对象的对象Class1
并尝试使用Main运行此属性时收到的错误是:
“错误1不可发音成员'ConsoleApplication1.Class1.X_ValueProperty'不能像方法一样使用。C:\ Users \ Qosmio \ AppData \ Local \ Temporary Projects \ ConsoleApplication1 \ Program.cs 14 15 ConsoleApplication1”
我完全理解此错误。IE,您不能像运行方法一样运行属性,但是看来我从中了解到的视频的演示者是可以做到的。(在1:20左右)。
在Main方法中,似乎他先调用对象,然后调用属性,然后在出现错误时在其中输入值。
任何人都可以识别我在做什么错吗?
class Class1
{
int x;
public int X_ValueProperty
{
set {
if (value <= 0)
{
throw new Exception("Value cannot be zero or less than zero.");
}
this.x = value;
}
get
{
return this.x;
}
}
}
这是错误的出处:
class Program
{
static void Main(string[] args)
{
Class1 z= new Class1();
//Error--> z.X_ValueProperty();
}
}
您必须像下面这样调用该属性:
Class1 cls = new Class1();
cls.X_ValueProperty();
请注意其属性而不是方法,您可以设置或获取类的属性的值。请首先正确阅读有关属性的信息。
您可以设置\获取像这样的值:
cls.X_ValueProperty = 25;
int x = cls.X_ValueProperty;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句