您的位置:首页 > 其它

边看书边做边发挥-图书软件-9

2016-03-06 15:34 323 查看
输出的目录还不够详细,应该还有第一章,第二章,第三章…,章节也有页码,节里还有1.1,1.2,1.3…的项目编号,添加这些成员,再添加一些重载的构造函数,更方便的根据这些属性初始化这些属性,然后ToString() 方法返回这些字段的组合

public class Chapter : BindingableBase
{
private string index;
...

public Chapter(string index, string name, int page, IEnumerable<Section> collection)
: this(index, name, page)
{
sections = new ObservableCollection<Section>(collection);
}

public string Index
{
get { return index; }
set { SetProperty(ref index, value); }
}
...

public override string ToString()
{
return index + " " + name + " " + page;
}
}


在 FrameworkDesignGuidelinesBookInitCatalogue 类给每章初始化对象时,添加编号,名称,页码

public Chapter NewChapterOne()
{
return new Chapter("第1章", "概述", 1,
new ObservableCollection<Section>
{
new Section("1.1","精心设计的框架所具备的品质", 2,
new ObservableCollection<Section>
{
new Section("1.1.1","精心设计的框架是简单的",2),
new Section("1.1.2","精心设计的框架代价高",3),
new Section("1.1.3","精心设计的框架充满利弊权衡",4),
new Section("1.1.4","精心设计的框架应该借鉴过去的经验",4),
new Section("1.1.5","精心设计的框架要考虑未来的发展",5),
new Section("1.1.6","精心设计的框架应具有良好的集成性",5),
new Section("1.1.7","精心设计的框架是一致的",7),
})
});
}


Chapter 类和 Section 类几乎一样,这时需要重新分配一下 Catalogue 类,Chapter 类和 Section 类的 GetEnumeratorTreeView() 方法的职责,修改他们的代码, Catalogue 类调用 Chapter 类的 GetEnumeratorTreeView() 方法,Chapter 类调用 Section 类的 GetEnumeratorTreeView() 方法,他有一个 padLeft 参数,缩进字符串,Section 类删除两个参数的GetEnumeratorTreeView() 重载方法,现在只需要一个参数的方法,这里的基本思想是,自身各个负责各个的集合,返回自己的名称加 Sections 属性的集合,这里有两方法返回,一种是先建立一个字符集合,添加自身对象和 Sections 属性构成一个新集合返回,Section 类需要递归,Chapter 类就不用了,他没有 Chapters 集合属性,而是 Sections 属性,另一种是不必先建立集合串集合,使用回调的方式迭代返回,这里使用第一种

Catalogue 类

public IEnumerable<string> GetEnumeratorTreeView()
{
var list = new List<string>();
foreach (var item in Items)
{
list.AddRange(item.GetEnumeratorTreeView());
}
return list;
}


Chapter 类

internal IEnumerable<string> GetEnumeratorTreeView()
{
var list = new List<string>();
list.Add(this.ToString());
var padLeft = "    ";
foreach (var item in sections)
{
list.AddRange(item.GetEnumeratorTreeView(padLeft));
}
return list;
}


Section 类,需要一个递归私有方法

internal IEnumerable<string> GetEnumeratorTreeView(string padLeft)
{
var list = new List<string>();
list.Add(padLeft + this);
foreach (var s in sections)
{
var newPadLeft = padLeft + "    ";
var nestList = Aggregate(newPadLeft, s);
list.AddRange(nestList);
}
return list;
}

private List<string> Aggregate(string padLeft, Section section)
{
var list = new List<string>();
list.Add(padLeft + section);
foreach (var s in section.sections)
{
var newPadLeft = padLeft + "    ";
var nestList = Aggregate(newPadLeft, s);
list.AddRange(nestList);
}
return list;
}


输出测试结果正常,但别的章节未编写编号



9686
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  net 图书