继承一个没有任何构造函数的抽象类 [英] Inherit an abstract class without any constructor

查看:156
本文介绍了继承一个没有任何构造函数的抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个类继承一个类,标记为abstract,没有定义任何构造函数。

I want to inherit a class from another class, marked as abstract, that not have any constructor defined.

这是我的代码:

// In one assembly (TheMessage.dll), as seen via F12 in VS (from Metadata)
namespace Namespace1 
{
    public abstract class Message
    {
       public string Body { get; set; }
       // some abstract methods here, not shown.
    }
}

// In another assembly (TheUser.dll)
namespace Namespace2
{
    public class MyMessage : Namespace1.Message
    {
         public MyMessage()
         {
         }
    }
}

问题是在 public MyMessage()构造函数中我收到错误

The problem is that on the public MyMessage() constructor I get the error


类型'Namespace1.Message'没有定义构造函数

The type 'Namespace1.Message' has no constructors defined

我在MSDN上看到继承摘要的网站(摘要(C#参考)第一个例子)没有构造函数的类是可能的。那么,有人知道我为什么会收到这个错误吗?

I saw on MSDN site (abstract (C# Reference) first example) that inheriting an abstract class without constructor is possible. So, anyone know why I get this error?

请注意,当类型与Message类位于同一个DLL中时,可以继承,并且它可以像程序集公开的那样工作一些其他类型派生自 Namespace1.Message 类似于以下内容:

Note that one can inherit just fine when type is in the same DLL as the Message class and it works as that assembly exposes some other types deriving from Namespace1.Message similar to following:

// The same assembly  as Message (TheMessage.dll), as seen via F12 in VS (from Metadata)
namespace Namespace3
{
    public class Message : Namespace1.Message
    {
        public Message() {}
        public Message(string to) {}
    }
}






我还查了类型'...'没有定义构造函数,但它没有谈论继承,而只是新建一个实例,我显然没有期望直接实例化一个实例一个抽象类。


I've also checked The type '...' has no constructors defined, but it does not speak about inheritance but rather just new-ing up an instance and I clearly have no expectations to directly instantiate an instance of an abstract class.

推荐答案

如果您无法看到消息类的实例构造函数(通常因为它们都是 private 并且你是在课外,或者他们都是私人内部而且你在集会之外),你不能写继承自消息的类。 (好吧,你可以创建两个实例构造函数,它们使用:this(...)语法循环链接,但这没有用。)

If none of the instance constructors of the Message class are visible to you (typically because they are all private and you are outside the class, or they are all private or internal and you are outside the assembly), you cannot write a class that inherits from Message. (Well, you could make two instance constructors which chain each other cyclicly with the :this(...) syntax, but that would not be useful).

请注意,当您查看元数据(为您所参考的程序集生成的反射生成伪C#)时,您通常只会看到可见成员,因此任何私人内部成员将不会显示。我认为你看一下元数据,因为我们看到了非抽象(和非外部)的方法,因为缺少了主体(只有一个分号; 而不是一个主体 {...} )。

Note that when you look at the "metadata" (reflection generated pseudo-C# for an assembly you refer), you typically only see the "visible" members, so any private or internal members will not show up. I think you look at the metadata because we see non-abstract (and non-extern) methods whoses bodies are absent (just a semicolon ; there instead of a body { ... }).

消息的源代码 class将有一个或多个构造函数,每个 private internal ,但是从程序集外部看时,这些是不存在。

The source code of your Message class will have one or more constructors, each private or internal, but when seen from outside the assembly, these are "non-existent".

如果非静态C#类的源代码消息不包含实例构造函数,编译器会自动生成一个。如果类是具体的(即非 abstratc ),并且<$ c,它将是 public 无参数构造函数$ c> protected 如果类是抽象的那么一个。

If the source code of a non-static C# class Message contains no instance constructors, the compiler will generate one automatically. It will be a public parameterless constructor if the class is concrete (i.e. non-abstratc), and a protected one if the class is abstract.

这意味着如果源代码如下所示:

That means that if the source code looks like this:

public abstract class Message
{
  // note: zero non-static constructors here
}

它的编译方式与它说的完全相同:

it will be compiled exactly as if it had said:

public abstract class Message
{
  protected Message()
  {
  }
}

在这种情况下,派生的所有类可以访问生成的实例构造函数

and in that case the generated instance constructor is accessible to all classes deriving from Message.

这篇关于继承一个没有任何构造函数的抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆