继承带有参数化构造函数的 class 以重用基本属性

inheriting a class with parameteraized constractor to reuse base properties

我正在尝试尽量减少在派生的 classes 中使用构造器,并有一个基础 class 来保存公共属性

所以我尝试了如下

public class sharedData
{
    public string GlobMeta;
    public int GlobValue;
    public sharedData(string meta, int value)
    {
        GlobMeta = meta;
        GlobValue= value;

    }
}
public class derivedData: sharedData
{
  public string test;
  test = string.Format("Shared meta = {0}, Shared Value = {1}",GlobMeta, GlobValue);
}

然后使用

var shared = new sharedData("desc", 1);
var derived = new derivedData();
var testData = derived.test;

所以当我创建派生的实例时,它将使用基本(共享)值 这是所有派生的 classes.

所共有的

我该如何实现这一点,因为我的想法是对大量数据集合使用更少的内存。以及干净的代码和易用性。

您声明了一个基于 class、sharedData 的无参数构造函数。因此,任何继承 class 的 class 都必须定义一个基础构造函数。您不能再依赖编译器为您完成工作了。

public class derivedData : sharedData
{
    public string test;

    public derivedData(string meta, int value) : base(meta, value)
    {
        // you can't do assignment in the class scope, unless it can be done statically, it has to be inside a method block
        test = string.Format("Shared meta = {0}, Shared Value = {1}", GlobMeta, GlobValue);
    }

    // or, if you prefer to have a parameterless ctor
    public derivedData() : base("a default value for meta", default(int))
    {
        test = string.Format("Shared meta = {0}, Shared Value = {1}", GlobMeta, GlobValue);
    }
}

编辑:您似乎将其用于全局设置,在这种情况下,您需要将 GlobMetaGlobValue 标记为静态。否则它们在 sharedData/derivedData.

的不同实例之间根本不会 "shared"

您应该考虑将其重写为:

public static class SharedData
{
    public static string Meta { get; private set; }
    public static int Value { get; private set; }

    public static void SetData(string meta, int value)
    {
        Meta = meta;
        Value = value;
    }
}
public class DerivedData
{
    public string Test
    {
        get { return string.Format("Shared meta = {0}, Shared Value = {1}", SharedData.Meta, SharedData.Value); }
    }
}

编辑 2:如果您需要这些的各种实例,请使用以下内容:

public class SharedData
{
    public string Meta { get; set; }
    public int Value { get; set; }

    public SharedData(string meta, int value)
    {
        Meta = meta;
        Value = value;
    }
}
public class DerivedData : SharedData
{
    public string Test
    {
        get { return string.Format("Shared meta = {0}, Shared Value = {1}", Meta, Value); }
    }


    public DerivedData(string meta, int value) : base(meta, value)
    {
    }

    // note: this is a copy ctor, changing data after this has been created, will not affect this.
    public DerivedData(SharedData data) : base(data.Meta, data.Value)
    {
    }
}

我和Xiaoy312的想法一样,在他发第二个例子的时候写下了这个:

public class SharedData
{
    internal static string GlobMeta;
    internal static int GlobValue;

    public SharedData(string meta, int value)
    {
        GlobMeta = meta;
        GlobValue = value;
    }

    public SharedData(){}
}

public class DerivedData: SharedData
{
    public DerivedData() : base()
    {
        Console.WriteLine("Shared meta = {0}, Shared Value = {1}", GlobMeta, GlobValue);
    }
}

将静态变量声明为 'internal 意味着它们只能在 'SharedData 和任何从它派生的 类 范围内访问。