public DerivedClass(string x) : base(x)
{
x="blah";
}
此代码会调用值为 x 的基本构造函数作为“blah”吗?
最佳答案
碱基调用总是先完成,但您可以让它调用静态方法。例如:
public Constructor(string x) : base(Foo(x))
{
// stuff
}
private static string Foo(string y)
{
return y + "Foo!";
}
现在如果你打电话
new Constructor("Hello ");
然后将使用“Hello Foo!”调用基础构造函数。
请注意,您不能在正在构造的实例上调用实例方法,因为它还没有“准备好”。
关于c# - 调用传递值的基本构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1012085/