繁体   English   中英

子/父关系的类编程方法

[英]Class programming approach for child/parent relationship

我正在开发一个从各种在线档案中删除内容的程序,但我是OOP的新手。 我认为最好的方法是拥有一个指定共享变量和方法的父类,然后是每个存档的子类,然后包含从该特定站点获取信息的特定方法,例如GrabStoryVariables()每个子类以满足该存档的个别需求。 该程序在文本框中获取一个URL,然后从那里它将确定使用哪个子类实例化的URL。

我遇到的问题是弄清楚如何创建子类对象并使其可供整个程序访问。 例如,要创建FanFictionAuthors的实例:FanBook:

private void btnGetInfo(object sender, EventArgs e)
{
  CreateBook();
}

private void CreateBook()
{
  if (addressBar.Text.Contains("fanficauthors.net"))
  {
    FanFictionAuthors myBook = new FanFictionAuthors();
  }
  return;
}

myBook的范围只是CreateBook()函数,所以这种方法不会起作用。 有关处理此问题的最佳方法的任何建议吗? 我正在使用它作为一种更好地学习编程的方法,所以“正确”的方式是我想要弄清楚的,不管是什么。

编辑:该程序的具体功能是从fanfiction.net,f​​ictionpress.com或任意数量的其他在线故事档案中获取在线故事的提供URL。 每个故事都有一组共享属性,例如标题,章节数,单词长度,章节标题以及故事的实际内容。 该程序编译所有这些以创建单个html文档(稍后将扩展为允许不同的电子书格式),而不是一堆小的单个章节文件。

考虑到这一点,每个存档之间应该有所不同的部分是从特定存档中获取变量的方法,以及如何根据存档的功能在章节之间进行迭代。

目前我正在做的只是在启动主窗体时立即创建myBook对象,然后为获取变量并执行迭代的函数创建不同的方法名称。 然而,随着我添加更多档案,这变得更加复杂。 我最初想做的是将myBook转换为各个归档类型(本例中为FanFictionAuthors)以获取使用其特定功能的能力。 从在线看,看起来从父母到孩子的铸造并不容易,也不推荐,所以我不确定如何处理这个问题。

这是该项目的GitHub链接。 这个版本稍微过时,但是让你看看我目前是如何接近这个: https//github.com/benroth/fBook

创建一个超类,其中包含常用属性和方法:

public class FanBook
{
    // use a common constructor
    public FanBook(string url)
    {
        grabHtml(url);
        // ...
    }

    protected string grabHtml(string address) { // SNIP }

    protected void CreateStoryHeader() { // SNIP }

    // other common methods which are the same for every subclass (maybe BuildToc, GetStory, etc.)

    // maybe if you want some easy access to attributes, you could add a dictionary
    public void Dictionary<string, string> Attributes;

    // Then use abstract methods to define methods that are different for subclasses
    protected abstract void GrabStoryVariables();
    protected abstract void GenerateStoryInfo();
}

然后创建一个派生自Book的子类:

public class FFNETBook : FanBook {
    // FFNETBook constructor to call contructor from FanBook too
    public FFNETBook(string url) : FanBook(url) {
        // specific initializations for FFNET

    }

    public override void GrabStoryVariables() { // special implementation for FFNET here }

    public override void GenerateStoryInfo() { // special implementation for FFNET here }
}

我知道当你没有太多的经验时,OOP很难掌握。 所以随时提问。 如果你做得对,那么你永远不需要转换为子类。

要回答评论中的问题:

您可以在form1.cs文件中创建一个类变量:

private FanBook currentBook;

private void CreateBook()
{
    currentBook = new FFNETFanBook("http://...");
}

private void AnotherMethod() {
    if ( currentBook != null ) {
        currentBook.GrabStoryVariables();
    } else {
        throw new Exception("Book not initialized yet.");
    }
}

实现名为IAuthors的接口并定义所需的方法

Interface IAuthors
{
//method
void authorMethod();
}

在您的类中实现接口

Public Class FanFictionAuthors:IAuthors
{
public void authorMethod()
{
//fanfiction specific action
}
}

Public class SciFiAuthors:IAuthors
{
public void authorMethod()
{
//scifiauthor specific action
}
}

现在,在现有代码中进行以下更改

private void btnGetInfo(object sender, EventArgs e)
{
  IAuthors auth=CreateBook();//Use a Interface ref
  auth.authorMethod();//Runtime will decide which authorMethod to call depending 
                      //on the object returned.
}

//Note that i am returning the Interface type instead of the void
private IAuthors CreateBook()
{
  if (addressBar.Text.Contains("fanficauthors.net"))
  {
    return new FanFictionAuthors();//Return your object
  }elseif(addressBar.Text.Contains("scificauthors.net"))
  {
    return new SciFiAuthors();//Return your object
  }
}

您可以在父级中创建一个列表,并将子级添加到该列表中。 然后你可以走一下这个列表,看看孩子是否已经完成等等。

ChildList.Add(new fan....)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM