小编Ram*_*Ram的帖子

C#中的构造函数

我有一个带有2个构造函数的父类,派生类试图用2种不同的方法调用父类的构造函数

public class Parent
{
    public Parent()
    {
        //some stuffs
    }
    public Parent(string st)
    {
        //some stuffs
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我有一个带有两个方法的派生类.我必须Parent在一个方法和Parent(string st)其他方法中使用-constructor .但是这里总是调用Parent-constructor.下面是派生类

public class Derived : Parent
{
    public void GetData()
    {
        //Here I initialize the constructor like this
        Parent p = new Parent();
    }

    public void GetData1()
    {
        string s = "1";
        Parent p = new Parent(s);
    }
}
Run Code Online (Sandbox Code Playgroud)

请让我告诉我如何实现这一目标.提前致谢.

c# constructor

3
推荐指数
1
解决办法
443
查看次数

标签 统计

c# ×1

constructor ×1