[英]Why can't we use a constructor with parameter in derived classes
Why is this not possible? 为什么这不可能呢? I get the following compiler-error when instantiating "DerivedClass" with a constructor-parameter:
使用构造函数参数实例化“ DerivedClass”时,出现以下编译器错误:
'GenericParameterizedConstructor.DerivedClass' does not contain a constructor that takes 1 argument 'GenericParameterizedConstructor.DerivedClass'不包含带有1个参数的构造函数
But calling a very similar method works. 但是调用非常相似的方法是可行的。
Why? 为什么?
class Program
{
static void Main(string[] args)
{
// This one produces a compile error
// DerivedClass cls = new DerivedClass("Some value");
// This one works;
DerivedClass cls2 = new DerivedClass();
cls2.SomeMethod("Some value");
}
}
public class BaseClass<T>
{
internal T Value;
public BaseClass()
{
}
public BaseClass(T value)
{
this.Value = value;
}
public void SomeMethod(T value)
{
this.Value = value;
}
}
public class DerivedClass : BaseClass<String>
{
}
Constructors aren't inherited - it's as simple as that. 构造函数不被继承-就这么简单。
DerivedClass
contains a single constructor - the public parameterless constructor provided by default by the compiler, because you haven't specified any constructors. DerivedClass
包含一个构造函数-编译器默认提供的公共无参数构造函数,因为您没有指定任何构造函数。
Note that this has nothing to do with generics. 请注意,这与泛型无关 。 You'd see the same thing if
BaseClass
weren't generic. 如果
BaseClass
不是通用的,您会看到同一件事。
It's easy to provide constructors for DerivedClass
though: 尽管很容易为
DerivedClass
提供构造函数:
public class DerivedClass : BaseClass<String>
{
public DerivedClass() : base()
{
}
public DerivedClass(string value) : base(value)
{
}
}
The deriving class needs to expose the constructor 派生类需要公开构造函数
public class DerivedClass : BaseClass<String>
{
public DerivedClass(string str) :base(str) {}
}
It would sometimes be helpful if there a way of instructing the compiler to automatically generate for a particular derived class constructors which precisely mimic and wrap all those of the base class. 如果有一种方法指示编译器自动为特定的派生类构造函数自动生成并精确模拟并包装所有基类的构造函数,则有时会很有帮助。 Having such behavior occur by default, however, would be problematic.
但是,默认情况下会发生这种行为,这将是有问题的。 Many derived classes expect that some of their code will be called whenever an instance of their type is created.
许多派生类期望在创建其类型的实例时会调用其某些代码。 Suppose a parent type had two constructors:
假设父类型具有两个构造函数:
parentType(int foo) {...} parentType(string foo) {...}
and a derived type had one: 派生类型有一个:
derivedType(string foo) {...}
What should be the effect of new derivedType(7);
new derivedType(7);
的作用是什么? ? ? The compiler would know how to create a
new baseType(7);
编译器会知道如何创建一个
new baseType(7);
, but if it created a new "blank" derivedType object and then simply called the parent-type constructor, the result would be a derivedType
object which had never run any of derivedType
's construction code. ,但是如果它创建了一个新的“空白” derivedType对象,然后简单地称为parent-type构造函数,则结果将是一个从未运行过
derivedType
的任何构造代码的derivedType
对象。 While some classes wouldn't have any problem with that (and for such classes, the earlier-mentioned hypothetical feature would be helpful), a lot of classes would. 尽管某些类对此没有任何问题(并且对于此类类,前面提到的假设功能会有所帮助),但很多类都可以。
Incidentally, a somewhat-related issue occurs with protected constructors. 顺便说一下,受保护的构造函数会出现一些相关的问题。 In some .net languages including at least the current version of C#, if non-abstract type
Foo
defines a protected constructor, that constructor may only be used to create instances of derived types. 在至少包括C#当前版本的某些.net语言中,如果非抽象类型
Foo
定义了受保护的构造函数,则该构造函数只能用于创建派生类型的实例。 In other languages, including the current vb.net, it's possible for code within a derived type to call a protected constructor of the base type to create a new base-type instance. 在其他语言(包括当前的vb.net)中,派生类型中的代码可以调用基本类型的受保护构造函数来创建新的基本类型实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.