C# 抽象类、构造函数和 Co
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/811247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Abstract class, constructors and Co
提问by undsoft
Well, I'm trying to reuse a portion of C# code. It's an abstract class with UDP server, which can be seen here:
好吧,我正在尝试重用一部分 C# 代码。它是一个带有UDP服务器的抽象类,可以在这里看到:
http://clutch-inc.com/blog/?p=4
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
- “TheProject.TheServer”不包含采用“1”参数的构造函数
- 'TheProject.UDPServer.Start()' 由于其保护级别而无法访问
- “TheProject.UDPServer”不包含采用“0”参数的构造函数
采纳答案by Tamas Czinege
Constructors do not get inherited in C#. You will have to chain them manually:
构造函数在 C# 中不会被继承。您必须手动链接它们:
public TheServer(int port)
: base(port)
{
}
Also, if Start is protected, you will have to create some sort of public method that calls it:
此外,如果 Start 受到保护,您将必须创建某种调用它的公共方法:
public void StartServer()
{
Start();
}
回答by Otávio Décio
You didn't define a TheServer constructor with one argument so you can't call TheServer(20501); you didn't define a zero arguments constructor for UDPServer but you defined one with one argument. Your two methods in TheServer are protected, hence the error on #2.
您没有使用一个参数定义 TheServer 构造函数,因此您无法调用 TheServer(20501); 您没有为 UDPServer 定义零参数构造函数,但您定义了一个带有一个参数的构造函数。你在 TheServer 中的两个方法受到保护,因此 #2 上的错误。
回答by John Saunders
Your derived class needs to add a one-parameter constructor, and delegate it to the base class:
您的派生类需要添加一个单参数构造函数,并将其委托给基类:
public TheServer(int port) : base(port) {}
Also, the Start
method is protected. You'll need your own:
此外,该Start
方法受到保护。你需要自己的:
public void StartMe(){base.Start();}
回答by Kurt Schelfthout
- and 3.
- 和 3。
Add a constructor to TheServer that calls the base constructor (of UDPServer); something like this:
向 TheServer 添加一个构造函数,该构造函数调用(UDPServer 的)基础构造函数;像这样:
public TheServer(int port) : base(port) {}
2 Check out the method Start on UDPServer: it is protected. This means that only subclasses of that class can call it.
2 查看方法在 UDPServer 上启动:它是受保护的。这意味着只有该类的子类才能调用它。
回答by Chad Grant
public class TheServer
{
public TheServer():base(port) {
}
}
var myServer = new TheServer(1337).Brings().All().The().Boys().to().The().Yard()
回答by Eoin Campbell
You'll need to post the code to your Abstract class but at a complete guess,
您需要将代码发布到您的抽象类,但完全猜测,
You've got a ctor
in your UDPServer class that you haven't implemented in your TheServer Class... You need something like...
你ctor
在你的 UDPServer 类中有一个你没有在你的 TheServer 类中实现的......你需要像......
public TheServer(int port) : base(port)
{
... specifics
}
And you've also forgotten to override the Start() method in your TheServer class but its marked as private in the underlying class... Your underlying class should have something like...
而且您还忘记覆盖 TheServer 类中的 Start() 方法,但它在底层类中标记为私有...您的底层类应该具有类似...
//In UDPServer
protected void Start()
{
//Code to start
}
//In TheServer
protected void StartTheServer()
{
base.Start();
}
回答by Noldorin
These errors actually have fairly straightforward causes:
这些错误实际上有相当简单的原因:
You haven't defined a constructor in your derived class (
TheServer
). Constructors (unlike methods of course) aren't automatically inherited, so you'll need to declare constructors that match thee ones in the parent class and chain them together using something like:public TheServer(int port) : base(port) { // Your code here. } public TheServer() : base() { // Your code here. }
The
Start
method is declared asprotected
in the base (UDPServer
) class. Either change the access modifier in the base class topublic
, or figure out a way to call the method from the derived class when you need to (the latter must have been intended by the writer ofUDPServer
).- Same reason as 1, except this is referring to the default (parameterless) constructor.
您尚未在派生类 (
TheServer
) 中定义构造函数。构造函数(当然与方法不同)不会自动继承,因此您需要声明与父类中的构造函数匹配的构造函数,并使用以下内容将它们链接在一起:public TheServer(int port) : base(port) { // Your code here. } public TheServer() : base() { // Your code here. }
该
Start
方法protected
在基UDPServer
类( ) 中声明。要么将基类中的访问修饰符更改为public
,要么想办法在需要时从派生类调用该方法(后者必须是 的编写者所设计的UDPServer
)。- 与 1 相同的原因,除了这是指默认(无参数)构造函数。
Hope that helps.
希望有帮助。
回答by Elliot
And, for what it's worth, I'd recommend using:
而且,对于它的价值,我建议使用:
UDPServer serv = new TheServer(20501);
serv.start();
Or, perhaps even more generic, Server. Depends on what methods you need to call on serv.
或者,也许更通用,服务器。取决于您需要在 serv 上调用哪些方法。