如何在派生类中调用基类构造函数?

8

根据一些需求,我想在不在派生类中编写方法/构造函数的情况下将基类的所有构造函数/方法添加到派生类中。

为此,我编写了以下代码,但它不起作用。 它显示错误“ConsoleApplication1.TestClass2'不包含一个带1个参数的构造函数”。
如何修复它?

我不能在基类中创建任何方法或构造函数。 除了继承一个类之外,还有其他方法吗?

我的代码:

namespace ConsoleApplication1
{
    public class TestClass1
    {
        public TestClass1()
        {
            Console.WriteLine("This is base class constructor1");
        }

        public TestClass1(string str1,string str2)
        {
            Console.WriteLine("This is base class constructor2");
        }

        public TestClass1(string str1,string str2,string str3)
        {
            Console.WriteLine("This is base class constructor3");
        }

        public TestClass1(string str1,string str2,string str3,string str4)
        {
            Console.WriteLine("This is base class constructor4");
        }
    }

    public class TestClass2 : TestClass1
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
            TestClass2 test = new TestClass2("test");
            TestClass2 test1 = new TestClass2("test,test");
        }
    }
}

1
你没有那么幸运。在C#中,构造函数不能被继承。你的派生类将不得不声明所有需要的构造函数。它可以使用:base(...)语法从基类链接构造函数。你的问题是为什么不能在派生类中使用带参数的构造函数的重复,并可能有其他类似的问题。 - Jeppe Stig Nielsen
1
如果您使用ReSharper,您可以在派生类中按Alt+Ins来生成构造函数。 - Eli Arbel
我有些困惑。基类应该有这些构造函数吗?但是你写道“我不能在基类中创建任何方法或构造函数。”你是想把它们添加到派生类中吗?但是你又写道“在派生类中不编写方法/构造函数”。请澄清一下。 - Zev Spitz
4个回答

17
不,你需要明确地为派生类添加构造函数。基类的构造函数只适用于基类。你需要链接你感兴趣的重载函数的构造函数。
public class TestClass2 : TestClass1
{
    public TestClass2 (string str1,string str2)
    : base(str1,str2)//Call base constructor explicitly
    {

    }
}
以上示例显示我特别关注接受两个字符串参数的构造函数重载。在这种情况下,你只能使用此重载,因为TestClass2没有定义任何其他构造函数。
当你定义一个构造函数时,编译器不会为你提供默认构造函数。所以你唯一创建TestClass2实例的方法是new TestClass2(arg1,arg2);

5

试试这个:

public class TestClass2 : TestClass1
{
    public TestClass2()
    {
    }

    public TestClass2(string str1, string str2)
        : base(str1, str2)
    {
    }

    public TestClass2(string str1, string str2, string str3)
        : base(str1, str2, str3)
    {
    }

    public TestClass2(string str1, string str2, string str3, string str4)
        : base(str1, str2, str3, str4)
    {
    }
}

1
你需要用单个参数,例如"test"和"test,test"来调用。
namespace ConsoleApplication1
{
    public class TestClass1
    {
        public TestClass1()
        {
            Console.WriteLine("This is base class constructor1");
        }


        public TestClass1(string str1, string str2)
        {
            Console.WriteLine("This is base class constructor2");
        }

        public TestClass1(string str1, string str2, string str3)
        {
            Console.WriteLine("This is base class constructor3");
        }

        public TestClass1(string str1, string str2, string str3, string str4)
        {
            Console.WriteLine("This is base class constructor4");
        }
    }

    public class TestClass2 : TestClass1
    {
        public TestClass2(string str)
        {
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            TestClass2 test = new TestClass2("test");
            TestClass2 test1 = new TestClass2("test,test");

        }
    }
}

0
 public TestClass2(string str):base(str) // will invoke base class constructor
 {
 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接