我有两个图形对象(比如某种Tables),我想设置它们的样式.琐碎的代码如下:
table1.BorderWidth = 2;
table1.BorderColor = Color.GloriousPink;
table2.BorderWidth = 2;
table2.BorderColor = Color.GloriousPink;
Run Code Online (Sandbox Code Playgroud)
(真正的代码有更多行.)
更聪明的方法是使用方法.
void Format Table(int tableIndex)
{
Table table;
if(tableIndex == 1)
table = table1;
if(tableIndex == 2)
table = table2;
table.BorderWidth = 2;
table.BorderColor = Color.GloriousPink;
}
Run Code Online (Sandbox Code Playgroud)
我正在考虑一种方法,使其更具可扩展性(if/ switch部分增长迅速),我想出了:
foreach(Table table in new List<Table> { table1, table2 })
{
table.BorderWidth = 2;
table.BorderColor = Color.GloriousPink;
}
Run Code Online (Sandbox Code Playgroud)
这个更短,并且可以非常简单地添加任何可能的附加表.它有什么缺点吗?
没有什么事情是错误的,但我会按照你原来的想法去实际把它放在一个方法中,而只是传入实际的表格.
public void Format(Table table)
{
table.BorderWidth = 2;
table.BorderColor = Color.GloriousPink;
}
foreach(Table table in tables)
{
Format(table);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |