简体   繁体   English

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

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

Based on some requirements, I want to add all constructors/methods of the base class into the derived class without writing methods/constructors in the derived class.基于一些需求,我想将基类的所有构造函数/方法都添加到派生类中,而无需在派生类中编写方法/构造函数。

For that I have written code as shown below but it does not work.为此,我编写了如下所示的代码,但它不起作用。 It shows error "ConsoleApplication1.TestClass2' does not contain a constructor that takes 1 arguments".它显示错误“ConsoleApplication1.TestClass2' 不包含带有 1 个参数的构造函数”。
How can I fix that?我该如何解决?

I cannot create any method or constructor in the base class.我无法在基类中创建任何方法或构造函数。 Is there any other way apart from inheriting a class?除了继承一个类还有别的方法吗?

My code:我的代码:

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

No, You'll have to explicitly add constructor to your derived classes.不,您必须向派生类显式添加构造函数。 Base class's constructor is for base class only.基类的构造函数仅用于基类。 You'll have to chain the constructor of your interested overload.您必须链接您感兴趣的重载的构造函数。

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

    }
}

Above sample shows that am particularly interested in constructor overload which takes two string parameters.上面的示例表明,我对采用两个字符串参数的构造函数重载特别感兴趣。 In this case you are only allowed to use this overload only since TestClass2 doesn't have any other constructors defined.在这种情况下,您只能使用此重载,因为TestClass2没有定义任何其他构造函数。

Compiler will not provide default constructor for you when you define one;当您定义一个构造函数时,编译器不会为您提供默认构造函数; So there is only way you can create instance of TestClass2 is new TestClass2(arg1,arg2);因此,您可以创建 TestClass2 实例的唯一方法是new TestClass2(arg1,arg2);

Try this:尝试这个:

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

you invoke with single parameter ie "test" and "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");

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

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

相关问题 在基类之前调用​​派生类构造函数 - Call derived class constructor before base class 如何从无参数派生类调用带参数的基类构造函数? - How to call parameterised base class constructor from parameterless derived class? 派生类显式基本构造函数调用 - Derived class explicit base constructor call 我应该如何使用从通用基础 class 派生的 class 调用基础 class 构造函数? - How should I call a base class constructor with a class derived from a generic base class? 让隐式派生类构造函数调用基类构造函数 - Letting implicit derived class constructor call base class constructor 是否可以从基类构造函数中调用派生类构造函数? - Is it possible to call a derived class constructor from a base class constructor? 如果派生类没有参数化构造函数,如何从派生类中调用基类的参数化构造函数? - How can I call a base class's parameterized constructor from a derived class if the derived class has no parameterized constructor? C#继承:当我调用派生类构造函数时,如何调用基类构造函数 - C# Inheritance: How to invoke the base class constructor when i call the derived class constructor 在派生类构造函数之后调用基类方法 - Call base class method after derived class constructor 如何在派生构造函数中设置基类的值 - How to set base class the values in the derived constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM