Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设计两个数据略有不同的类_C# - Fatal编程技术网

C# 设计两个数据略有不同的类

C# 设计两个数据略有不同的类,c#,C#,假设我的第一个类有一个公共属性,它返回要导出的文件的头。例如: private readonly List<string> fileHeaders = new List<string> { "Last Name", "First Name", "Middle Name", "Suffix", "Degree", }; public List<string> FileHeaders {

假设我的第一个类有一个公共属性,它返回要导出的文件的头。例如:

private readonly List<string> fileHeaders = new List<string>
{
    "Last Name", 
    "First Name", 
    "Middle Name", 
    "Suffix", 
    "Degree",           
};

public List<string> FileHeaders
{
    get { return fileHeaders; }
}
private readonly List fileHeaders=新列表
{
“姓氏”,
“名字”,
“中间名”,
“后缀”,
“学位”,
};
公共列表文件头
{
获取{return fileHeaders;}
}
现在我有一个
类B
,它导出的文件头的唯一区别是它还有一个列头要导出,例如一个地址列


什么是好的面向对象设计可以做到这一点

使
fileHeaders
成为基类的受保护成员。比如:

public class Base
{
    protected List<string> fileHeaders;
    public List<string> FileHeaders
    {
        get
        {
            return fileHeaders;
        }
    }
}

public class A : Base
{
    public A()
    {
        fileHeaders = new List<string>
        {"Last Name", "First Name", "Middle Name", "Suffix", "Degree"};
    }
}

public class B : Base
{
    public B()
    {
        fileHeaders = new List<string>
        {"Last Name", "First Name", "Middle Name", "Suffix", "Degree", "Something Else"};
    }
}
public class A
{
    protected readonly List<string> fileHeaders = new List<string>
    {
        "Last Name", "First Name", "Middle Name", "Suffix", "Degree", 
    };

    public List<string> FileHeaders
    {
        get
        {
            return fileHeaders;
        }
    }
}

public class B : A
{
    public B()
    {
        fileHeaders.Add("Something Else");
    }
}

既然两个实例只在数据上不同,为什么不创建一个类,将列表作为成员和只读属性(如上所述),并在构造函数中使用参数初始化它呢?不需要不同的类。

我更喜欢这个带有接口和抽象基类的解决方案,它在派生类中强制实现

这涵盖了实体开闭原理

public interface IFileHeaders
{
    IList<string> GetFileHeaders();
}

public abstract class FileHeaderBase : IFileHeaders
{
    protected abstract IList<string> FileHeaders { get; }

    public IList<string> GetFileHeaders() => FileHeaders;
}
公共接口IFILHEADERS
{
IList GetFileHeaders();
}
公共抽象类FileHeaderBase:IFileHeaders
{
受保护的抽象IList文件头{get;}
public IList GetFileHeaders()=>FileHeaders;
}
这包括一个基本的实现:

public class FileHeaderList1 : FileHeaderBase
{
    private readonly IList<string> _fileHeaders = new List<string>
    {
        "Last Name" ,
        "First Name" ,
        "Middle Name" ,
        "Suffix" ,
        "Degree" ,
    };

    protected override IList<string> FileHeaders => _fileHeaders;        
}
公共类FileHeaderList1:FileHeaderBase
{
私有只读IList\u fileHeaders=新列表
{
“姓氏”,
“名字”,
“中间名”,
“后缀”,
“学位”,
};
受保护的覆盖IList文件头=>\u文件头;
}
这包括延迟加载:

public class FileHeaderList2 : FileHeaderBase
{
    private IList<string> _fileHeaders;

    protected override IList<string> FileHeaders
    {
        get
        {
            if ( _fileHeaders == null )
            {
                _fileHeaders = new List<string>
                {
                    "Address" ,
                };
            }
            return _fileHeaders;
        }
    }
}
公共类FileHeaderList2:FileHeaderBase
{
私有IList_文件头;
受保护的覆盖IList文件头
{
得到
{
if(_fileHeaders==null)
{
_fileHeaders=新列表
{
“地址”,
};
}
返回文件头;
}
}
}
这包括依赖项注入:

public class FileHeaderList3 : FileHeaderBase
{
    private readonly IList<string> _fileHeaders;

    public FileHeaderList3( IList<string> fileHeaders )
    {
        _fileHeaders = fileHeaders;
    }

    protected override IList<string> FileHeaders => _fileHeaders;
}
公共类FileHeaderList3:FileHeaderBase
{
私有只读IList_文件头;
公共文件头List3(IList文件头)
{
_fileHeaders=fileHeaders;
}
受保护的覆盖IList文件头=>\u文件头;
}

使
文件头成为基类的受保护成员。让这两个类都从基类继承,但要更改
fileHeaders