Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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#,在决定如何最好地用C#(.NET3.5)描述某个数据结构时,我遇到了几个问题 我必须创建一个类库,为命令行程序的输入创建一些文本文件。有几十种文件类型,但大多数都有类似的页眉、页脚和一些其他属性 因此,文件可能如下所示: timestamp filename company ....lots of attributes that make up the file body.... footerdetail1 footerdetail2 footerdetail3 在这几十种文件类型中,实际

在决定如何最好地用C#(.NET3.5)描述某个数据结构时,我遇到了几个问题

我必须创建一个类库,为命令行程序的输入创建一些文本文件。有几十种文件类型,但大多数都有类似的页眉、页脚和一些其他属性

因此,文件可能如下所示:

timestamp filename company

....lots of attributes that make up the file body....

footerdetail1 footerdetail2 footerdetail3
在这几十种文件类型中,实际上只有三种唯一的页眉/页脚组合

因此,我想创建一个包含大多数headerItems和footeritems的父类,然后在派生类中实现实际的文件类型:

class BaseFileType {

public List<HeaderItems>; 
public List<BodyItems>;   
public List<FooterItems>;
FileType filetype;
String filename;

public BaseFileType1 (FileType filetype,String filename) {

   this.filetype = filetype;
   this.filename = filename;


   // add stuff to the header list
   // add stuff to the footer list


} 

    // some methods to find items in the header/ footer lists

}

class ActualFile1 : BaseFileType {

    ActualFile() {


    //add stuff to the bodyitems list
    }


    //some other generic methods


} 
类BaseFileType{
公开名单;
公开名单;
公开名单;
文件类型文件类型;
字符串文件名;
公共BaseFileType1(文件类型FileType,字符串文件名){
this.filetype=filetype;
this.filename=文件名;
//向标题列表添加内容
//将内容添加到页脚列表
} 
//在页眉/页脚列表中查找项目的一些方法
}
类ActualFile1:BaseFileType{
实际文件(){
//将内容添加到bodyitems列表中
}
//其他一些通用方法
} 
问题是,我想从派生类的基类继承构造函数,但从我所读到的内容来看,这是无法做到的。这里最好的策略是什么

我已经看到,我可以从派生类构造函数调用基构造函数,如下所示:

ActualFile():基(父参数)

这是我完成任务的最好方式吗?最后,我只是在为所有这些文件信息寻找最佳的数据结构,所以在创建这些类时,我不需要重复我自己


还有什么办法可以代替人的事情呢?有一个单独的FileFactory来生成包含我需要的结构的类吗?

正如您所说,构造函数不是继承的

如果在构造过程中需要知道这些值,那么强制派生类在构造函数中提供详细信息将是理想的情况。如果没有,请考虑派生类必须提供的抽象属性:

// base-class
public abstract FileType FileType {get;}

// derived-class
public override FileType FileType {
    get {return FileType.Excel;} // or whatever
}
请注意,除非您非常清楚地记录它(并理解其含义),否则不应该在构造函数内调用虚方法(如上面所述)(因为派生类型的构造函数尚未执行),但之后就可以了


因为文件名实际上不依赖于子类型,所以我怀疑不管是哪种方式,都会把它作为一个cor参数。

您可以让您的基类读取页眉和页脚,并提供对中间属性的访问。然后定义几十个特定属性读取器将实现的接口。接口可能需要接受一个“基类”,而每个实现将定义中间的内容是如何被读取的。
实际上,您有一种读取公共部分的通用方法和访问属性的通用方法。这些部分是松散耦合的,因此更改的限制性更小。

这看起来像是模板方法可以解决的问题。。。(最后是使用图案的位置:)

接下来,对于3个唯一组合中的每一个,中间级别的类,如

abstract public class UniqueHeaderFooterCombination1 : DocumentGenerator
{
  protected override void AddHeader()
  {
    Console.WriteLine("unique combination1 header");
  }
  protected override void AddFooter()
  {
    Console.WriteLine("unique combination1 footer");
  }
}
最后,为每个唯一的文件类型指定具体的类

public class LetsSayCSVGenerator : UniqueHeaderFooterCombination1
{
  protected override void AddBody()
  {
    Console.WriteLine("csv body items");
  }
}
将此与工厂结合,为文件类型返回正确类型的生成器,就完成了

abstract class DocumentGeneratorFactory
{
  public static DocumentGenerator GetGenerator(FileType eFileType)
  {
    switch (eFileType)
    {
      // a hash of FileType => Generator
      default:
        return new LetsSayCSVGenerator();
    }

  }
}

class Program
{
  static void Main(string[] args)
  {
    DocumentGenerator d = DocumentGeneratorFactory.GetGenerator(FileType.CSV);
    FileInfo f = d.Generate("ItsAlive.CSV");
  }
}
abstract class DocumentGeneratorFactory
{
  public static DocumentGenerator GetGenerator(FileType eFileType)
  {
    switch (eFileType)
    {
      // a hash of FileType => Generator
      default:
        return new LetsSayCSVGenerator();
    }

  }
}

class Program
{
  static void Main(string[] args)
  {
    DocumentGenerator d = DocumentGeneratorFactory.GetGenerator(FileType.CSV);
    FileInfo f = d.Generate("ItsAlive.CSV");
  }
}