C# 复杂 class 构造函数

C# complex class constructors

我有以下系统:名为 DataClass 的 "mother" class 具有属性,其中两个是 classes、PartClassMemberClass。特别是,MemberClass 具有属性,其中之一是 class、SideClass - 具有自己的属性。

这是代码示例:

using System;

namespace ConsoleApp
{
    public class DataClass
    {
        public int num { get; set; }
        public string code { get; set; }
        public PartClass part { get; set; }
        public MemberClass member { get; set; }

        public DataClass()
        {
            PartClass part = new PartClass();
            MemberClass member = new MemberClass();
        }
    }

    public class PartClass
    {
        public int seriesNum { get; set; }
        public string seriesCode { get; set; }

        public PartClass() { }
    }

    public class MemberClass
    {
        public int versionNum { get; set; }
        public SideClass side { get; set; }

        public MemberClass()
        {
            SideClass side = new SideClass();
        }
    }

    public class SideClass
    {
        public string firstDetail { get; set; }
        public string secondDetail { get; set; }
        public bool include { get; set; }

        public SideClass() { }
    }

现在,我正在尝试初始化 DataClass 并为所有属性赋值,但这不起作用。 因此,DataClass "owns" PartClassMemberClass 以及 MemberClass 本身 "sees" SideClass 这是底部 class 并且有点独立于所有人。 这里是其余代码:

class Program
{

    static void Main(string[] args)
    {
    DataClass myData = new DataClass()
    {
        num = 13,
        code = "message",
        //from here downwards nothing works; error..
        part.seriesNum = 1,
        part.seriesCode = 7,
        member.versionNum = 9,
        member.side.firstDetail = "pass",
        member.side.secondDetail = "checked",
        member.side.include = true;
    }
}

}

我认为通过安装构造函数和属性 DataClass 实例化不会有问题,但实际上 DataClass 没有看到任何重要的属性(指的是 class 的属性es)。 有人可以帮我吗?谢谢..

使用以下内容:

DataClass myData = new DataClass()
{
    num = 13,
    code = "message",
    part = new PartClass()
    {
        seriesNum = 1,
        //here down nothing works; error
        seriesCode = "abc"
    },
    member = new MemberClass()
    {
        versionNum = 9,
        side = new Side()
        {
            firstDetail = "pass",
            secondDetail = "checked",
            include = true
        }
    }
};

您应该创建一个 DataClass 实例,然后初始化字段

DataClass myData = new DataClass();

myData.num = 13,
myData.code = "message",
myData.part.seriesNum = 1,
myData.part.seriesCode = 7,
myData.member.versionNum = 9,
myData.member.side.firstDetail = "pass",
myData.member.side.secondDetail = "checked",
myData.member.side.include = true;

为了不必每次都手动创建 Member Class 和 Part Class 的实例,您可以使用 get 和 set 方法创建实例。

public class DataClass
{
    public int num { get; set; }
    public string code { get; set; }

    private PartClass _part;
    public PartClass part { get { if (_part == null) _part = new PartClass(); return _part; } set { _part = value; } }


    private MemberClass _member;
    public MemberClass member { get { if (_member == null) _member = new MemberClass(); return _member; } set { _member = value; } }

}