namespace ConsoleApplication18
{
class Program
{
class MyClass
{
public int age;
public string name;
public static MyClass createobj() {
MyClass returnit = new MyClass();
returnit.age = 45;
returnit.name = "Homer Simpson";
return returnit;
}
}
static void Main(string[] args)
{
MyClass homer = createobj();
Console.WriteLine(homer.age);
}
}
}
这是错误消息:为什么此代码不能在类中使用静态方法?
The name 'createobj' does not exist in the current context
为什么我收到此错误消息,即使我做了CREATEOBJ()方法静态? 我试过MyClass homer = MyClass.createobj();
,它没有返回任何错误,程序运行良好?为什么不能使用MyClass homer = createobj();
?
因为你不是在那个班上。 – SLaks
我不明白。 – user3292196