我正在尝试创建一个基类,用于以键/值方式存储数据。基类将生成密钥并负责存储数据。当继承基类时,派生类可以指定它自己的键,但基类必须知道派生类的键。例如:
public class ParentClass
{
private string key = "Parent";
public void GenerateKey()
{
Console.WriteLine(key);
}
}
public class FeatureClass : ParentClass
{
public string key = "Feature";
}
public class SubFeatureClass : FeatureClass
{
public string key = "SubFeature";
}
FeatureClass feature = new FeatureClass();
feature.GenerateKey(); //I would like this to produce "Parent_Feature"
SubFeatureClass subFeature = new SubFeatureClass();
subFeatureClass.GenerateKey(); //I would like this to generate "Parent_Feature_SubFeature"
无论发生了多少级别的继承,如何让基类知道其子级键。
答案 0 :(得分:4)
您可以通过制作基本方法virtual
并在覆盖中引用它来完成此操作:
public class ParentClass
{
public virtual string key {get {return "Parent";}}
public void GenerateKey()
{
Console.WriteLine(key);
}
}
public class FeatureClass : ParentClass
{
public override string key {get{return base.key + "_Feature";}}
}
public class SubFeatureClass : FeatureClass
{
public override string key {get{return base.key + "_SubFeature";}}
}
答案 1 :(得分:0)
可以使用抽象或虚函数来实现这一点(哪一个取决于ParentClass
是否适合作为实际类型或是否总是需要派生类。
在任何情况下,您都将拥有一个虚拟(或抽象)函数,您可以在派生类中重写该函数以返回特殊键。实际上,在C#中,您可以拥有虚拟/抽象属性:
public abstract class ParentClass
{
private string key = "Parent";
protected abstract string derivedKey
{
get;
}
public void GenerateKey()
{
Console.WriteLine(key + derivedKey);
}
}
public class FeatureClass : ParentClass
{
public override string derivedKey
{
get
{
return "Feature";
}
}
}
答案 2 :(得分:0)
您可以使用virtual
属性
public class ParentClass
{
public virtual string Key
{
get
{
return "Parent";
}
}
}
public class FeatureClass : ParentClass
{
public override string Key
{
get
{
return base.Key + "_" + "Feature";
}
}
}
public class SubFeatureClass : FeatureClass
{
public override string Key
{
get
{
return base.Key + "_" + "SubFeature";
}
}
}
然后,您可以这样使用它,
FeatureClass feature = new FeatureClass();
Console.WriteLine(feature.Key); // <-- "Parent_Feature"
SubFeatureClass subFeature = new SubFeatureClass();
Console.WriteLine(subFeature.Key); // <-- "Parent_Feature_SubFeature"
答案 3 :(得分:0)
使用虚拟财产而不是字段:
public class ParentClass
{
public ParentClass(){Key = "Parent";}
private virtual string Key {get;set;}
public void GenerateKey()
{
Console.WriteLine(Key);
}
}
public class FeatureClass : ParentClass
{
public FeatureClass(){Key = base.Key + "_" + "Feature";}
override string Key {get;set;}
}
答案 4 :(得分:0)
public class ParentClass
{
protected List<string> keys = new List<string>();
public ParentClass()
{
keys.Add("Parent");
}
public void GenerateKey()
{
Console.WriteLine(string.Join("_", keys));
}
}
public class FeatureClass : ParentClass
{
public FeatureClass()
{
keys.Add("Feature");
}
}
public class SubFeatureClass : FeatureClass
{
public SubFeatureClass()
{
keys.Add("SubFeature");
}
}
答案 5 :(得分:0)
在我的版本中使用构造函数重载和构造函数链接:
public class ParentClass
{
private string key;
protected ParentClass(string childKeys)
{
key = !string.IsNullOrEmpty(childKeys) ? key + "Parent_" + childKeys : key;
}
public void GenerateKey()
{
// Get keys from subclasses
Console.WriteLine(key);
}
}
public class FeatureClass : ParentClass
{
public FeatureClass() : base("Feature") { }
protected FeatureClass(string key) : base("Feature_" + key) { }
}
public class SubFeatureClass : FeatureClass
{
public SubFeatureClass() : base("SubFeature") { }
protected SubFeatureClass(string key) : base("SubFeature_" + key) { }
}
public class ReallySubFeatureClass : SubFeatureClass
{
public ReallySubFeatureClass() : base("ReallySubFeature") { }
}
测试代码看起来与您的相似,但我添加了一个级别,例如:
class Program
{
static void Main(string[] args)
{
FeatureClass feature = new FeatureClass();
feature.GenerateKey(); //I would like this to produce "Parent_Feature"
SubFeatureClass subFeature = new SubFeatureClass();
subFeature.GenerateKey(); //I would like this to generate "Parent_Feature_SubFeature"
ReallySubFeatureClass reallySubFeature = new ReallySubFeatureClass();
reallySubFeature.GenerateKey();
Console.ReadKey();
}
}
<强>结果:强>