抽象类作为引用参数 - 编译器错误
我在 VS2010 上有这个简单的示例:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
AbsClass absClass = new ConClass();
// I have tried this also and the error is different:
// ConClass absClass = new ConClass();
absClass.Id = "first";
Console.WriteLine(absClass.Id);
MyMethod(ref absClass); // <<- ERROR.
Console.WriteLine(absClass.Id);
Console.ReadKey();
}
public void MyMethod(ref AbsClass a)
{
a.Id = "new";
}
}
public abstract class AbsClass
{
public string Id { get; set; }
}
public class ConClass : AbsClass { }
}
我想知道为什么它不能正确构建。
I have this simple sample on VS2010:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
AbsClass absClass = new ConClass();
// I have tried this also and the error is different:
// ConClass absClass = new ConClass();
absClass.Id = "first";
Console.WriteLine(absClass.Id);
MyMethod(ref absClass); // <<- ERROR.
Console.WriteLine(absClass.Id);
Console.ReadKey();
}
public void MyMethod(ref AbsClass a)
{
a.Id = "new";
}
}
public abstract class AbsClass
{
public string Id { get; set; }
}
public class ConClass : AbsClass { }
}
I would like to know why this cannot build right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将
MyMethod
设为静态:问题不在于抽象类,“问题”在于静态
Main
方法。静态方法没有实例,因此无法调用实例方法。静态类和静态成员的 msdn 。
You need to make your
MyMethod
static:The problem isn't the abstract class, the "problem" is the static
Main
method. Static methods don't have an instance, and as such, can't call instance methods.msdn on static classes and static members.
您需要将
MyMethod
方法设置为静态:或者最好创建
Program
类的实例,并从该实例调用MyMethod
:第一个方法有效的原因是
Main
方法被标记为静态,并且不绑定到Program
类的实例。 .NET Framework CLR 在程序集中搜索名为Main
的静态方法,该方法采用String
数组,并使该函数成为入口点。您会注意到,许多教程甚至 MSDN 代码示例都使用 static 关键字标记Program
类,当类中的所有方法仅包含静态方法时,这被认为是最佳实践。第二种方法有效以及首选此方法的原因是因为您将
MyMethod
定义为实例方法。基本上,您需要一个对象的实例才能执行实例方法;new
关键字创建指定类型的实例。静态方法可以在没有对象实例的情况下调用,但也不能访问任何非静态实例成员(属性、私有/公共变量等)。通常,您希望避免静态方法和类,除非您必须实现实用程序类、利用扩展方法或提供辅助方法。You either need to make your
MyMethod
method static:Or preferrably, create an instance of the
Program
class, and callMyMethod
from that instance:The reason why the first method works is because the
Main
method is marked static, and isn't tied to an instance of theProgram
class. The .NET Framework CLR searches through your assembly for a static method namedMain
that takes an array ofString
, and makes that function the entry point. You'll notice that a lot of tutorials and even MSDN code samples mark theProgram
class with the static keyword, which is considered best practice when all of the methods in a class contain only static methods.The reason why the second method works, and why this method is preferred, is because you defined
MyMethod
to be an instance method. Basically, you need an instance of an object in order to execute an instance method; thenew
keyword creates an instance of a specified type. Static methods can be called without an instance of an object, but also cannot access any non-static instance members (properties, private/public variables, etc). Generally, you want to avoid static methods and classes unless you must implement a utility class, utilize extension methods, or provide helper methods.