[英]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.