下面是我对C#代码有疑问的部分。
class Program
{
class DOB { int d, m, y; }
int a;
enum Month { jan, feb, mar };
static void Main(string[] args)
{
a = 6; //Showing error
DOB d = new DOB(); //DOB is not static; still no error.
Month m = 0; //Month is non static but not showing error(I know it cannot be static)
Console.WriteLine(m);
Console.ReadKey();
}
}变量(一个)的赋值显示错误,因为它是一个非静态成员。同时,道布和enum 月类也是非静态的,但它不是时间误差。
发布于 2014-09-24 05:35:55
不能从静态类或方法访问实例成员。
a是Program的一个实例字段,因此在a = 6上会出现一个错误。
DOB d = new DOB()只需创建DOB类的一个新对象,并将其赋值给一个局部变量。
Month m = 0还创建了一个新的局部变量。
如果你写了
[...]
DOB d;
static void Main(string[] args)
{
d = new DOB();您将得到与a相同的错误。
https://stackoverflow.com/questions/26009176
复制相似问题