【发布时间】:2011-06-25 01:39:27
【问题描述】:
我正在创建一个需要打印 HTML 字符串和 HTML 文档的打印机类。所以基本上可以得到:
Printer.Print("<b>Hello world</b>");
和
Printer.Print(@"C:\hello.html");
因此,在设计我的课程时,我在以下之间做出了 Print 方法定义:
public static void Print(string inputString, string mode){
if(mode=="htmlString"){//Print the string itself}
else if(mode=="htmlFile"){//Print the document in the filepath}
}
或者
public static void Print(string inputString){
if(file.Exists(inputString)){//Print the document in the filepath}
else{//Print the string itself}
}
一般来说,哪种做法更好?第一个选项需要另一个不太好的参数,但是如果我们使用第二个选项,如果我们打算实际打印一个文件但使用了不正确的文件名,它将打印错误的东西。
【问题讨论】:
-
为用户构建外观看起来很聪明,但是构建执行多种不同操作的方法可能会变得非常混乱,而它们的名称并不能告诉您它们究竟做了什么。在这种情况下,打印一个简单的字符串和打开和关闭一个文件有很大的不同(和影响)。首先,只包含文件名纯文本的 HTML 怎么样?在您的示例中,我无法将简单的 filePath 打印为字符串。
-
好点,从这个角度我并没有想太多。
-
我会推荐失望先生的回答,读起来很清楚,并且对于某些事情应该如何表现没有任何困惑。
标签: c# methods arguments class-design