抽象类作为引用参数 - 编译器错误

发布于 2024-10-28 10:59:09 字数 813 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请叫√我孤独 2024-11-04 10:59:09

您需要将 MyMethod 设为静态:

    public static void MyMethod(ref AbsClass a)
    {
        a.Id = "new";
    }

问题不在于抽象类,“问题”在于静态 Main 方法。静态方法没有实例,因此无法调用实例方法。

静态类和静态成员的 msdn

You need to make your MyMethod static:

    public static void MyMethod(ref AbsClass a)
    {
        a.Id = "new";
    }

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.

离去的眼神 2024-11-04 10:59:09

您需要将 MyMethod 方法设置为静态:

public static MyMethod(ref AbsClass a)
{
    a.Id = "new";
}

或者最好创建 Program 类的实例,并从该实例调用 MyMethod

Program p = new Program();
p.MyMethod(ref abs);

:第一个方法有效的原因是 Main 方法被标记为静态,并且不绑定到 Program 类的实例。 .NET Framework CLR 在程序集中搜索名为 Main 的静态方法,该方法采用 String 数组,并使该函数成为入口点。您会注意到,许多教程甚至 MSDN 代码示例都使用 static 关键字标记 Program 类,当类中的所有方法仅包含静态方法时,这被认为是最佳实践。

第二种方法有效以及首选此方法的原因是因为您将 MyMethod 定义为实例方法。基本上,您需要一个对象的实例才能执行实例方法; new 关键字创建指定类型的实例。静态方法可以在没有对象实例的情况下调用,但也不能访问任何非静态实例成员(属性、私有/公共变量等)。通常,您希望避免静态方法和类,除非您必须实现实用程序类、利用扩展方法或提供辅助方法。

You either need to make your MyMethod method static:

public static MyMethod(ref AbsClass a)
{
    a.Id = "new";
}

Or preferrably, create an instance of the Program class, and call MyMethod from that instance:

Program p = new Program();
p.MyMethod(ref abs);

The reason why the first method works is because the Main method is marked static, and isn't tied to an instance of the Program class. The .NET Framework CLR searches through your assembly for a static method named Main that takes an array of String, and makes that function the entry point. You'll notice that a lot of tutorials and even MSDN code samples mark the Program 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; the new 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文