gpt4 book ai didi

VS2005 中的 C# : will this() execute the base constructor code for an inherited class?

转载 作者:行者123 更新时间:2023-11-30 19:33:42 24 4
gpt4 key购买 nike

对于 VS2005 中的 C#,在继承类中调用 this() 会导致基构造函数的执行吗?

编辑:如何避免重写 xy 作业?请注意,我希望 MyObject(int num) 构造函数执行 base() 构造函数。

public class MyObject : MyParentObject { 

int x;
int y;
int z;

public MyObject() {
x = 5;
y = 10;
}

public MyObject(int num) : base(num) {
x = 5;
y = 10;
z = num;
}

最佳答案

base()将由第一个构造函数隐式调用以在派生类中运行:

public MyObject() {
x = 5;
y = 10;
}

public MyObject(int setZ) : this() {
z = setZ;
}

相当于:

public MyObject() : base() {
x = 5;
y = 10;
}

public MyObject(int setZ) : this() {
z = setZ;
}

关于VS2005 中的 C# : will this() execute the base constructor code for an inherited class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3146172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com