在构造函数中调用基本构造函数和其他构造函数
标题可能听起来令人困惑。 我想要的是在构造函数内调用同一个类的构造函数和基类的构造函数。 也许我第一次尝试解决这个问题可以解释我的问题:
public MyClass(MyClass obj) : base(obj),this() {}
但是这个符号不起作用。 有什么办法可以解决这个问题吗?
Title may sound confusing.
What I want is to call a constructor of the same class and the constructor of the base class inside a constructor.
Maybe my first attempt to solve that may explain my question:
public MyClass(MyClass obj) : base(obj),this() {}
But that notation isn't working.
Is there any solution to solve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,您不能这样做,原因如下:
当构造函数调用其基类的构造函数时,后一个调用是相关构造函数的一部分。因此,您不能调用同一类的另一个构造函数和基类的构造函数,因为前一个调用已经包含对基构造函数的调用 - 您不能两次初始化基类
No, you can't do it for the following reason:
When a constructor calls the constructor of its base, the latter call is THE PART of the constructor in question. So you can't call another constructor of the same class AND a constructor of a base class because the former call already contains a call to a base constructor - you can't initialize your base twice
C# 允许使用 base(...) 或 this(...),因为每个构造函数最终都必须调用超级构造函数。因此,如果可以同时调用 base() 和 this(),则会发生两次超级构造函数调用,这从根本上来说是不正确的。
这与不可能调用 base(...) 两次的原因相同。
C# allows either base(...) or this(...), because every constructor must eventually invoke a super constructor. So if it were possible to invoke both base() and this(), two super constructors invocations would take place, which would be fundamentally incorrect.
It is the same reason why it is not possible to invoke base(...) twice.
对此没有技术解决方案,您需要一个解决方法,将逻辑移出默认构造函数,使基调用成为虚拟函数,而不是在超类中重写。
No technical solution for this, you need a workaround, move logic out of the default constructor, make the base call a virtual function than override at your super class.
一种方法是使
MyClass
的无参数构造函数调用base(obj)
。但如果你并不总是想这样做,那么我认为没有什么好办法。
也许你可以尝试在这些构造函数中移动东西。您能否发布有关这些构造函数中实际发生的情况的更多信息?
One way would be to make the parameterless constructor of
MyClass
to callbase(obj)
.But if you don't always want to do that, than I don't think there is a good way.
Maybe you can try to move stuff around in those constructors. Can you post more info about what actually happens in these constructors?
您可以简单地将构造函数代码复制到给定的构造函数中。或者创建一个从默认构造函数和此构造函数调用的特殊 setup() 函数。
You could simply copy the constructor code into the given constructor. Or make a special setup() function that is called from the default constructor and this constructor.
在 C# 中,您还可以在声明中为每个成员提供默认初始化:
这解决了许多使用其他构造函数的问题。
In C# you may also provide a default initialization in the declaration for each member:
This solves for many use-other-constructor issues.
其实你可以这样做,
您可以从子类实例激发父类构造函数,具体操作方法如下:
现在,假设您正在从学生类创建一个对象,
所以,你会这样做:
students std = new Students("Jack", 23);
这会将“Jack”发送给学生类构造函数,23 将通过 :base(age) 发送给 Person 构造函数
希望有帮助,
干杯。
Actually, you CAN do so,
You can fire the parent class constructor from the child class instance, here is how you do it:
Now, lets say you are creating an object from students class,
So, you will do this:
students std = new students ("Jack", 23);
This will send "Jack" to students class constructor, and 23 will be send to Person constructor via :base(age)
Hope this help,
Cheers.