繁体   English   中英

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

[英]How to call base class constructor in derived class?

基于一些需求,我想将基类的所有构造函数/方法都添加到派生类中,而无需在派生类中编写方法/构造函数。

为此,我编写了如下所示的代码,但它不起作用。 它显示错误“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");
        }
    }
}

不,您必须向派生类显式添加构造函数。 基类的构造函数仅用于基类。 您必须链接您感兴趣的重载的构造函数。

public class TestClass2 : TestClass1
{
    public TestClass2 (string str1,string str2)
    : base(str1,str2)//Call base constructor explicitly
    {

    }
}

上面的示例表明,我对采用两个字符串参数的构造函数重载特别感兴趣。 在这种情况下,您只能使用此重载,因为TestClass2没有定义任何其他构造函数。

当您定义一个构造函数时,编译器不会为您提供默认构造函数; 因此,您可以创建 TestClass2 实例的唯一方法是new TestClass2(arg1,arg2);

尝试这个:

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)
    {
    }
}

您使用单个参数调用,即“测试”和“测试,测试”

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");

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM