2013-03-18 114 views
-1

我有这两个功能如何重载两种方法具有不同的输入参数

private void calcResults() 
{ 
    MakePath(id, results, _resultCount); 
    MakePath(id, "XYZ", _resultSICount) 
} 

private string MakePath(string subFolder, object obj, int index) 
{ 
    string dir = System.IO.Path.Combine(_outputDir, subFolder); 
    string fileName = string.Format("{0} {1} {2}.xml", 
      obj.GetType().Name, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString()); 
    return System.IO.Path.Combine(dir, fileName); 
} 

private string MakePath(string subFolder, string tempFileName, int index) 
{ 
    string dir = System.IO.Path.Combine(_outputDir, subFolder); 
    string fileName = string.Format("{0} {1} {2}.xml", 
      tempFileName, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString()); 
    return System.IO.Path.Combine(dir, fileName); 
} 

可以请一些一个扶。

感谢

+6

你有什么问题/你有问题..? – MethodMan 2013-03-18 14:26:24

+2

在标题中,你讲的是重写*方法,但是没有一个显示的方法是'虚拟'的,所以在这种情况下重写是不可能的。 – 2013-03-18 14:28:52

+0

我需要先避免冗余代码..然后我想使用方法重写来简化代码。请让我知道如果你需要更多的信息 – user175084 2013-03-18 14:31:16

回答

6

如果我正确理解你的问题,你的意思是你想重载方法,以避免代码重复...这是我如何去做。

private void calcResults() 
    { 
     MakePath(id, results.GetType(), _resultCount); 
     MakePath(id, "XYZ", _resultSICount) 
    } 

    private string MakePath(string subFolder, Type type, int index) 
    { 
     return MakePath(subFolder, type.Name, index); 
    } 

    private string MakePath(string subFolder, string tempFileName, int index) 
    { 
     string dir = System.IO.Path.Combine(_outputDir, subFolder); 
     string fileName = string.Format("{0} {1} {2}.xml", 
       tempFileName, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString()); 
     return System.IO.Path.Combine(dir, fileName); 
    } 

我会避免使用对象作为你的第二个参数的类型,因为它似乎在这种情况下含糊,使用类型指示参数的预期目的。

+0

谢谢你这么多先生..正是我想要的..我对不够清楚的道歉.. – user175084 2013-03-18 14:55:14

2

你可以想想这样的事情:

private string MakePath(string subFolder, object obj, int index) 
    {   
     //tempFileName here is created beased on the TYPE of the object passed 
     string tempFileName = obj.GetType().Name; 
     return MakePath(subFolder,tempFileName , index); 

    } 

    private string MakePath(string subFolder, string tempFileName, int index) 
    { 
     //combine directory path 
     string dir = System.IO.Path.Combine(_outputDir, subFolder); 

     //compute final file name based on the several 
     //parameters and tempFileName parameter 
     string fileName = string.Format("{0} {1} {2}.xml", 
      tempFileName, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString()); 

     return System.IO.Path.Combine(dir, fileName); 
    } 

下面的代码所呈现的逻辑,纠正我,如果我错了,这2个methos之间的唯一区别是,在第一个tempFileName是基于类型名称,而第二个则是调用者传递的第二个参数。

+0

是的,你是对的。但是我想利用这里最重要的概念..你能提出一些建议吗..非常感谢 – user175084 2013-03-18 14:34:47

+0

@ user175084没有什么可以重写,它似乎全部在一个类中。你可能正在寻找的是“多态性”,这正是答案中的内容。 – 2013-03-18 14:35:37

+1

@ user175084你的意思是“重载”而不是“覆盖”。 – 2013-03-18 14:48:29

0

这里的问题是,字符串也是一个对象,所以编译器不能选择使用什么方法。您可以更改参数顺序,或重命名其中一种方法。

+1

编译器会不会为字符串选择字符串版本,因为它更合适? – 2013-03-18 14:38:33

+1

编译器会为'string'选择'string'版本,而为其他任何东西选择'object'版本。这里没有冲突。 – 2013-03-18 14:47:46

0
private string MakePath(string subFolder, object obj, int index) 
{ 
    if(obj.GetType()==typeof(string)) 
    { 
     //copy the strong typed version to here; 
     return; 
    } 
    string dir = System.IO.Path.Combine(_outputDir, subFolder); 
    string fileName = string.Format("{0} {1} {2}.xml", 
     obj.GetType().Name, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString()); 
    return System.IO.Path.Combine(dir, fileName); 
} 

然后,您可以使用一个函数来执行两个任务。这会做什么?

+2

我会在'if'里面的'object'中创建一个'string',然后让控制权继续。 – 2013-03-18 14:34:31

0

您可以在“结果”值转换为一个对象:路由到一个与目标参数去的GetValue得到的

class Program 
{ 
    static void Main(string[] args) 
    { 

     var ret1 = GetValue("String"); 
     Console.WriteLine(ret1); 
     var ret2 = GetValue((object)"test"); 
     Console.WriteLine(ret2); 

     Console.ReadKey(); 
    } 

    private static object GetValue(string p0) 
    { 
     return p0; 
    } 

    private static object GetValue(object p0) 
    { 
     return "Object"; 
    } 
} 

第二个电话。

相关问题