gpt4 book ai didi

c# - 如何获取基类的 "new"参数?

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:13 24 4
gpt4 key购买 nike

我从这样的无参数构造函数类派生类:

public class Base
{
public Base(Panel panel1)
{

}
}

public class Derived : Base
{
public Derived() : base(new Panel())
{
//How do I use panel1 here?
}
}

如何在 Derived 中引用 panel1?

(欢迎使用简单的解决方法。)

最佳答案

Adil 的回答假定您可以修改 Base。如果你不能,你可以这样做:

public class Derived : Base
{
private Panel _panel;

public Derived() : this(new Panel()) {}

private Derived(Panel panel1) : base(panel1)
{
_panel = panel1;
}
}

关于c# - 如何获取基类的 "new"参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14039567/

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