在构造函数中调用基本构造函数和其他构造函数

发布于 2024-11-15 16:50:01 字数 187 浏览 1 评论 0原文

标题可能听起来令人困惑。 我想要的是在构造函数内调用同一个类的构造函数和基类的构造函数。 也许我第一次尝试解决这个问题可以解释我的问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

囚我心虐我身 2024-11-22 16:50:01

不,您不能这样做,原因如下:

当构造函数调用其基类的构造函数时,后一个调用是相关构造函数的一部分。因此,您不能调用同一类的另一个构造函数和基类的构造函数,因为前一个调用已经包含对基构造函数的调用 - 您不能两次初始化基类

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

_畞蕅 2024-11-22 16:50:01

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.

不喜欢何必死缠烂打 2024-11-22 16:50:01

对此没有技术解决方案,您需要一个解决方法,将逻辑移出默认构造函数,使基调用成为虚拟函数,而不是在超类中重写。

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.

桃扇骨 2024-11-22 16:50:01

一种方法是使 MyClass 的无参数构造函数调用 base(obj)
但如果你并不总是想这样做,那么我认为没有什么好办法。

也许你可以尝试在这些构造函数中移动东西。您能否发布有关这些构造函数中实际发生的情况的更多信息?

One way would be to make the parameterless constructor of MyClass to call base(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?

煮酒 2024-11-22 16:50:01

您可以简单地将构造函数代码复制到给定的构造函数中。或者创建一个从默认构造函数和此构造函数调用的特殊 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.

月棠 2024-11-22 16:50:01

在 C# 中,您还可以在声明中为每个成员提供默认初始化:

private int foo_ = 41;

这解决了许多使用其他构造函数的问题。

In C# you may also provide a default initialization in the declaration for each member:

private int foo_ = 41;

This solves for many use-other-constructor issues.

随遇而安 2024-11-22 16:50:01

其实你可以这样做,
您可以从子类实例激发父类构造函数,具体操作方法如下:

public class Person
{
    public Person(int age) {
        this.Age = age; 
    } 
    public int Age;
}

public class students :Person
{
    public students(string name, int age) :base(age ) 
    { 
        this.studentName = name;
        this.Age = age;
    }

    public string studentName;
}

现在,假设您正在从学生类创建一个对象,
所以,你会这样做:

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:

public class Person
{
    public Person(int age) {
        this.Age = age; 
    } 
    public int Age;
}

public class students :Person
{
    public students(string name, int age) :base(age ) 
    { 
        this.studentName = name;
        this.Age = age;
    }

    public string studentName;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文