构造函数 (组件类 怎么添加构造函数就出错了,附代码)

来源:百度知道 编辑:UC知道 时间:2024/09/24 08:30:45
namespace WindowsApplication2
{
public partial class Component1 : Component
{
public static string link_str1;
public Component1():base()
{

}
public Component1(string link_str):base(link_str)
{
InitializeComponent();

}

public Component1(IContainer container)
{
container.Add(this);

InitializeComponent();
}
}
}

1、不要听楼上的,构造函数可以重载的,析构函数才不能重载,不过因为.net引用了GC,析构函数好像没啥用了。
2、不知道你使用哪个版本的FW?我在2008的MSDN下,看到Component对象,只有一个构造函数,并没有参数。

所以
public Component1(string link_str):base(link_str)

这行是错误的,因为基类并没有带有一个string类型参数的构造函数。

改成

public Component1(string link_str)
{
InitializeComponent();

}

试试。

PS:当基类的构造函数没有参数时,可以不必显式的调用基类构造函数,在需要显式调用基类的指定签名的构造函数时,才需要显式调用,并传递相应参数。

一个类构造函数只能有一个
想要继承父类,在一个构造函数上写完,不要多写。

构造函数是可以重载的,但是

public Component1(string link_str):base(link_str)

这个是错误的,因为Component类中并没有string这样一个参数的重载,所以应该为

public Component1(string link_str):base()

public Component1(string link_str)

public Component1(string link_str):base(link_str) 这个函数有问题
去掉后面的:base(link_str)