我有一个Command
class
我要解析为string / from string,将其作为UDP
消息发送。基类看起来很简单:
public abstract class Command
{
public Command() {}
public abstract override string ToString();
public abstract void FromString(string str);
}
从这里我得到classes
喜欢
public class NetCommand : Command
{
public string details;
public NetCommand() {};
public override string ToString() {return details;}
public override void FromString(string str) {details = str; }
}
或者更复杂的MovementCommand : Command
包含解析后的x
,y
,z
doubles
等等。
如何在基础class
中添加string
并使用FromString
方法的构造函数?
对于所有派生类,它将具有相同的确切代码,它将如下所示:
public Command(string str)
{
this.FromString(str);
}
并且每个class
将使用自己的FromString
方法从字符串中填充自己的成员。
但我迷失了语法,有什么帮助吗?
答案 0 :(得分:2)
这正是您所要求的,包括对抽象类的更改:
public abstract class Command
{
public Command() { }
public Command(string str)
{
FromString(str);
}
public abstract override string ToString();
public abstract void FromString(string str);
}
public class NetCommand : Command
{
public string details;
public NetCommand() {}
public NetCommand(string str) : base(str) { }
public override string ToString() { return details; }
public override void FromString(string str) { details = str; }
}
答案 1 :(得分:0)
我理解你的方式是你想要一个基类构造函数来创建一个子类对象。你不能这样做。
你可以做的是在基类上创建一个基于给定字符串创建具体对象的静态工厂方法,例如
public abstract class Command
{
public Command() {}
public abstract override string ToString();
public abstract void FromString(string str);
public static Command FromString(string str){
Command command;
if(/*string indicates it is a NetCommand*/){
command = new NetCommand();
command.FromString(str);
}
}
}
答案 2 :(得分:0)
有一个支持字段,用于存储通过构造函数传递的值,然后是两个设置并返回该值的虚拟方法。请注意,在下面的实现中,NetCommand
只能使用非参数构造函数进行实例化。
public abstract class Command
{
private string _details;
protected Command()
{
}
protected Command(string details)
{
_details = details;
}
public new virtual string ToString()
{
return _details;
}
public virtual void FromString(string details)
{
_details = details;
}
}
public class NetCommand : Command
{
public NetCommand(string str) : base(str)
{
}
}
答案 3 :(得分:0)
我建议这个实现(尽可能多的代码放入基类中以便于进一步创建类)
public abstract class Command {
protected Command(): this(null) {
}
protected Command(String details) {
Details = details;
}
// I'd rather convert it into property
public String Details {
get;
set; // may be "private set" will be better here
}
public override string ToString() {
return Details;
}
// TODO: think on delete or redesign this method:
// Redundant: Details provide all the functional required
// Bad name: "FromString" usually means create/return
// instance by given details
// public static Command FromString(string details)
public virtual void FromString(String details) {
Details = details;
}
}
...
public class NetCommand: Command {
public NetCommand(string details) : base(details) {
}
}
答案 4 :(得分:0)
扩展Domysee的答案,使用通用工厂方法,您可以执行以下操作:
public abstract class Command
{
protected Command() {}
public abstract override string ToString();
protected abstract void FromString(string str);
public static T FromString<T>(string str) where T: Command, new()
{
var command = new T();
command.FromString(str);
return command;
}
}
现在,只要派生的Command具有默认构造函数,您就可以使用Command.FromString创建它的实例,如下所示:
var command = Command.FromString<NetCommand>("command string");