有了Table,我们对其继承,实现更符合业务要求的类
SaleReport对外封装了对Table的处理细节,使用SaleReport的用户不会感觉到在使用Table类
1
/// <summary>
2
/// 描述一个销售报表
3
/// </summary>
4
public class SaleReport : Table
5
{
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
public SaleReport(string reportName)
8
: base(reportName, new ColumnCollection())
9
{
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
this.Columns.Add("序号", typeof(int), 0);
12
this.Columns.Add("姓名", typeof(string), null);
13
this.Columns.Add("商品名称", typeof(string), null);
14
this.Columns.Add("日期", typeof(DateTime), null);
15
this.Columns.Add("数量", typeof(double), null);
16
}
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
/// <summary>
19
/// 报表的名称
20
/// </summary>
21
public string Name
22
{
23
get
24
{
25
return this.Name;
26
}
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
/// <summary>
30
/// 加入数据
31
/// </summary>
32
/// <param name="staff"></param>
33
/// <param name="commodity"></param>
34
/// <param name="cash"></param>
35
public void AddRecord(string staff, string commodity, double cash)
36
{
37
Row row = this.NewRow();
38
row["序号"] = this.Rows.Count + 1;
39
row["姓名"] = staff;
40
row["商品名称"] = commodity;
41
row["日期"] = DateTime.Now;
42
row["数量"] = cash;
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
this.Rows.Add(row);
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
/// <summary>
48
/// 移除数据
49
/// </summary>
50
/// <param name="index"></param>
51
public void RemoveAt(int index)
52
{
53
this.Rows.RemoveAt(index);
54
}
55![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
/// <summary>
57
/// 返回销售报表的销售金额
58
/// </summary>
59
/// <returns></returns>
60
public double GetTotal()
61
{
62
double cash = 0;
63
foreach (Row row in this.Rows)
64
{
65
cash += (double)row["数量"];
66
}
67
return cash;
68
}
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
/// <summary>
71
/// 返回销售报表的销售金额
72
/// </summary>
73
/// <param name="staff">销售员</param>
74
/// <returns></returns>
75
public double GetTotal(string staff)
76
{
77
double cash = 0;
78
foreach (Row row in this.Rows)
79
{
80
if ((string)row["姓名"] == staff)
81
{
82
cash += (double)row["数量"];
83
}
84
}
85
return cash;
86
}
87![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
SaleReport对外封装了对Table的处理细节,使用SaleReport的用户不会感觉到在使用Table类
1
SaleReport report = new SaleReport("销售台帐");
2
report.AddRecord("Alex", "Phone", 2600);
3
report.AddRecord("Alex", "PC", 4560);
4
report.AddRecord("Alex", "Table", 234);
5
report.AddRecord("Sidney", "Phone", 2100);
6
report.AddRecord("Sidney", "TV", 4500);
7
report.AddRecord("Tom", "oven", 300);
8
report.AddRecord("Leo", "oven", 240);
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
report.Print();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行的结果是
序号 姓名 商品名称 日期 数量
1 Alex Phone 2007-2-13 23:19:27 2600
2 Alex PC 2007-2-13 23:19:27 4560
3 Alex Table 2007-2-13 23:19:27 234
4 Sidney Phone 2007-2-13 23:19:27 2100
5 Sidney TV 2007-2-13 23:19:27 4500
6 Tom oven 2007-2-13 23:19:27 300
7 Leo oven 2007-2-13 23:19:27 240