摘要类,构造函数和Co [英] Abstract class, constructors and Co

查看:123
本文介绍了摘要类,构造函数和Co的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我试图重用一部分C#代码。它是一个有UD​​P服务器的抽象类,可以在这里看到:

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.


  1. 'TheProject.TheServer'不
    包含一个构造函数需要'1'
    参数

  2. 'TheProject.UDPServer.Start()'是
    由于其保护而无法访问
    级别

  3. 'TheProject.UDPServer'
    不包含接受
    '0'参数的构造函数


推荐答案

p>构造函数不在C#中继承。您必须手动链接它们:

Constructors do not get inherited in C#. You will have to chain them manually:

public TheServer(int port) 
 : base(port)
{
}

此外,如果Start是受保护的,的公共方法调用它:

Also, if Start is protected, you will have to create some sort of public method that calls it:

public void StartServer()
{
    Start();
}

这篇关于摘要类,构造函数和Co的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆