【发布时间】:2012-12-28 06:52:43
【问题描述】:
如果我尝试声明具有相同参数的静态和非静态方法,编译器会返回错误:类型“Test”已经定义了具有相同参数类型的名为“Load”的成员。
class Test
{
int i = 0;
public int I
{
get { return i; }
set { i = value; }
}
public bool Load(int newValue)
{
i = newValue;
return true;
}
public static Test Load(int newValue)
{
Test t = new Test();
t.I = newValue;
return t;
}
据我所知这两种方法不能混合使用,非静态方法是在对象上调用,而静态方法是在类上调用,那么为什么编译器不允许这样的事情,有没有办法做类似的事情?
【问题讨论】:
-
名称或参数类型不能相同,编译器不在乎一个是静态的。