抽象类,构造函数和Co [英] Abstract class, constructors and Co
问题描述
好吧,我想重用的C#代码的一部分。它是一个抽象类UDP服务器,在这里可以看到:
的 http://clutch-inc.com/blog/?p=4
我已经创建了一个派生类是这样的:
公共类TheServer:UDPServer
{
保护覆盖无效PacketReceived(UDPPacketBuffer缓冲)
{
}
保护覆盖无效PacketSent(UDPPacketBuffer缓冲区,诠释bytesSent)
{
}
}
和我的应用程序我创建了派生类的一个实例是这样的:
TheServer SERV =新TheServer(20501);
serv.Start();
但我有错误,我真的不明白为什么。请帮助。
- 'TheProject.TheServer不
包含一个构造函数'1'
参数 - 'TheProject.UDPServer.Start()'是
无法访问,因为它保护
级 - 'TheProject.UDPServer没有
不包含一个构造函数
'0'参数
构造没有得到继承了C#。你将不得不链他们手动:
公共TheServer(INT端口)
:基地(端口)
{
}
此外,如果开始是受保护的,你必须建立某种形式公共方法的调用它:
公共无效startserver的()
{
开始();
}
Well, I'm trying to reuse a portion of C# code. It's an abstract class with UDP server, which can be seen here:
http://clutch-inc.com/blog/?p=4
I've created a derived class like this:
public class TheServer : UDPServer
{
protected override void PacketReceived(UDPPacketBuffer buffer)
{
}
protected override void PacketSent(UDPPacketBuffer buffer, int bytesSent)
{
}
}
And in my app I've created an instance of the derived class like this:
TheServer serv = new TheServer(20501);
serv.Start();
But I've got errors and I don't really understand why. Please help.
- 'TheProject.TheServer' does not contain a constructor that takes '1' arguments
- 'TheProject.UDPServer.Start()' is inaccessible due to its protection level
- 'TheProject.UDPServer' does not contain a constructor that takes '0' arguments
Constructors do not get inherited in C#. You will have to chain them manually:
public TheServer(int port)
: base(port)
{
}
Also, if Start is protected, you will have to create some sort of public method that calls it:
public void StartServer()
{
Start();
}
这篇关于抽象类,构造函数和Co的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!