C#中的抽象构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2299037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Abstract constructor in C#
提问by AndreyAkinshin
Possible Duplicate:
Why can't I create an abstract constructor on an abstract C# class?
可能的重复:
为什么我不能在抽象 C# 类上创建抽象构造函数?
Why I can't declare abstract an constructor of my class like this:
为什么我不能像这样声明抽象类的构造函数:
public abstract class MyClass {
public abstract MyClass(int param);
}
采纳答案by tvanfosson
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")
{
...
}
}
回答by Zach Johnson
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.
你不能声明它abstract
,但你可以在抽象类上有一个构造函数;只需删除这个词abstract
并为其提供一个正文。
回答by Michael Bray
By definition, the class can't be instantiated directly, so in a sense, it already is abstract.
根据定义,类不能直接实例化,所以从某种意义上说,它已经是抽象的。
回答by Daniel Earwicker
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?
构造函数不是普通的方法。它有一个特殊的目的,因此仅限于对这个目的有意义的语言特性。另请参阅:为什么构造函数不返回值?
回答by Davy Landman
Because abstract constructors are not supported.
因为不支持抽象构造函数。
But a abstract class can have a constructor.
但是抽象类可以有一个构造函数。
回答by Sergey Teplyakov
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.
在这种情况下,您强制所有派生类调用基类构造函数。
回答by Ben Voigt
Abstract implies virtual. A non-default constructor can never be called polymorphically, so virtual and abstract are not allowed on constructors.
抽象意味着虚拟。非默认构造函数永远不能被多态调用,因此构造函数上不允许使用 virtual 和 abstract 。
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.
如果在 C# 的未来版本中,泛型被增强以允许通过泛型类型参数调用非默认构造函数,那么对构造函数的多态调用将是可能的,并且还可能添加虚拟和抽象构造函数。
回答by ewernli
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 constructorand abstract static method. That's why you can also not specify constructor and static method in interfaces.
构造函数和静态方法永远不会动态(虚拟地)调度——您总是知道实例化的具体类型或静态方法的具体类。这就是为什么拥有抽象构造函数和抽象静态方法是没有意义的。这就是为什么您也不能在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 reflectionis 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.
注意:在类是常规对象的 Smalltalk 等语言中,您可以覆盖静态方法并具有抽象构造函数。但它不适用于 Java,因为类不是“常规”对象,即使您可以通过反射获取它们。