C# 中的抽象构造函数

发布于 2024-08-22 14:18:51 字数 352 浏览 7 评论 0原文

可能的重复:
为什么我无法创建抽象 C# 类上的抽象构造函数?

为什么我不能像这样声明我的类的抽象构造函数:

public abstract class MyClass {
    public abstract MyClass(int param);
}

Possible Duplicate:
Why can’t I create an abstract constructor on an abstract C# class?

Why I can't declare abstract an constructor of my class like this:

public abstract class MyClass {
    public abstract MyClass(int param);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

巴黎夜雨 2024-08-29 14:18:51

构造函数仅适用于定义它们的类,即它们不能被继承。使用基类构造函数(您必须调用其中之一,即使只自动调用默认构造函数),但不会被派生类覆盖。您可以在抽象基类上定义构造函数——它不能直接使用,但可以通过派生类调用。您不能做的是强制派生类实现特定的构造函数签名。

定义一个构造函数(通常为受保护的构造函数)是完全合理的,以便为所有派生类定义一些通用的设置代码。当抽象类提供一些依赖于此设置的其他默认行为时,也许尤其如此。例如:

public abstract class Foo
{
     public string Name { get; private set; }

     protected Foo( string name )
     {
         this.Name = name;
     }
}

public class Bar : Foo
{
     public Bar() : base("bar")
     {
        ...
     }
}

Constructors are only applicable to the class in which they are defined, that is, they are not inherited. Base class constructors are used (you have to call one of them, even if only calling the default one automatically) but not overridden by deriving classes. You can define a constructor on an abstract base class -- it can't be used directly, but can be invoked by deriving classes. What you can't do is force a derived class to implement a specific constructor signature.

It is perfectly reasonable to have a constructor defined, typically as protected, in order to define some common set up code for all derived classes. This is especially true, perhaps, when the abstract class provides some other default behavior which relies on this set up. For example:

public abstract class Foo
{
     public string Name { get; private set; }

     protected Foo( string name )
     {
         this.Name = name;
     }
}

public class Bar : Foo
{
     public Bar() : base("bar")
     {
        ...
     }
}
放赐 2024-08-29 14:18:51

你不能将它声明为抽象类,但你可以在抽象类上有一个构造函数;只需删除“abstract”一词并为其提供正文即可。

You can't declare it abstract, but you can have a constructor on your abstract class; just remove the word abstract and provide a body for it.

薄荷港 2024-08-29 14:18:51

构造函数更接近静态方法而不是“常规”方法。与静态方法一样,它们可以重载,但不能重写。也就是说,它们不是继承的,而是可以重新定义的。

public BaseClass
{
   public BaseClass( String s ) { ... }
   public static void doIt ( String s ) { ... }
}

public SubClass extends BaseClass
{
   public SubClass( String s )  { ... }
   public static void doIt ( String s ) { ... }
}

public SubClass2 extends BaseClass
{
}

new SubClass( "hello" );
SubClass.doIt( "hello" ); 

new SubClass2( "hello" ); // NOK
SubClass2.doIt( "hello" ); // NOK

构造函数和静态方法永远不会动态调度(实际上)——您始终知道实例化的具体类型或静态方法的具体类。这就是为什么拥有抽象构造函数抽象静态方法没有意义。这就是为什么您也不能在接口中指定构造函数和静态方法。

您甚至可以将构造函数视为静态工厂方法(并查看相应模式):

  MyClass obj = new MyClass(); // the way it is
  MyClass obj = MyClass.new(); // think of it like this

我认为定义抽象构造函数或抽象静态方法有意义的唯一情况是使用反射。在这种情况下,您可以确保所有子类都会重新定义相应的静态方法或构造函数。但反射是另一个话题...

注意:在像 Smalltalk 这样的语言中,类是常规对象,您可以覆盖静态方法并具有抽象构造函数。但它不适用于 Java,因为类不是“常规”对象,即使您可以通过反射获取它们。

Constructors are closer to static methods rather than "regular" methods. Like static methods, they can be overloaded, but not overriden. That is, they are not inherited but can be redefined.

public BaseClass
{
   public BaseClass( String s ) { ... }
   public static void doIt ( String s ) { ... }
}

public SubClass extends BaseClass
{
   public SubClass( String s )  { ... }
   public static void doIt ( String s ) { ... }
}

public SubClass2 extends BaseClass
{
}

new SubClass( "hello" );
SubClass.doIt( "hello" ); 

new SubClass2( "hello" ); // NOK
SubClass2.doIt( "hello" ); // NOK

Constructors and static methods are never dispatched dynamically (virtually) -- You always know the concrete type you instantiate or the concrete class of the static method. That's why it makes no sense to have abstract constructor and abstract static method. That's why you can also not specify constructor and static method in interfaces.

You can even think of constructor as static factory method (and see the corresponding pattern):

  MyClass obj = new MyClass(); // the way it is
  MyClass obj = MyClass.new(); // think of it like this

The only case I see where it would make sense to define abstract constructor or abstract static method would be if reflection is used. In this case, you could ensure that all subclass would redefine the corresponding static method or constructor. But reflection is another topic...

Note: in languages such as Smalltalk where classes are regular objects, you can override static method and have abstract constructor. But it doesn't apply to Java because classes are not "regular" objects even if you can get them with reflection.

殤城〤 2024-08-29 14:18:51

抽象意味着虚拟。非默认构造函数永远不能被多态调用,因此构造函数上不允许使用 virtual 和 abstract。

如果在 C# 的未来版本中,泛型得到增强,允许通过泛型类型参数调用非默认构造函数,那么对构造函数的多态调用将成为可能,并且还可以添加虚拟和抽象构造函数。

Abstract implies virtual. A non-default constructor can never be called polymorphically, so virtual and abstract are not allowed on constructors.

IF in a future version of C#, generics are enhanced to allow calling non-default constructors through a generic type parameter, then polymorphic calls to constructors would be possible and virtual and abstract constructors might be added as well.

嘿嘿嘿 2024-08-29 14:18:51

这是什么问题:

public abstract class MyClass {
    protected MyClass(int param)
    {
    }
}

在这种情况下,您强制所有派生类调用基类构造函数。

What wrong with this:

public abstract class MyClass {
    protected MyClass(int param)
    {
    }
}

In this case you oblige all derived classes to call base class constructor.

浮云落日 2024-08-29 14:18:51

因为不支持抽象构造函数。

但抽象类可以有构造函数。

Because abstract constructors are not supported.

But a abstract class can have a constructor.

番薯 2024-08-29 14:18:51

构造函数不是普通的方法。它有一个特殊的目的,因此仅限于对该目的有意义的语言功能。另请参阅:为什么构造函数不返回值?

A constructor is not an ordinary method. It has a special purpose, and so is restricted to language features that make sense for that purpose. See also: Why do constructors not return values?

坏尐絯 2024-08-29 14:18:51

根据定义,该类不能直接实例化,因此从某种意义上说,它已经是抽象的。

By definition, the class can't be instantiated directly, so in a sense, it already is abstract.

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