2011-02-03 56 views
2

是否有可能做这样的事情:方法参数和过载

class Program 
{ 
    static void Main(string[] args) 
    { 
     Customer1 c1 = new Customer1(); 
     DoSomething(c1); 

     Customer2 c2 = new Customer2(); 
     DoSomething(c2); 
    } 

    static void DoSomething<T>(T customer) 
    { 
     //... code here ... 
     InitializeCustomer(customer); // <- error indeed :-(
     //... code here ... 
    } 

    static void InitializeCustomer(Customer1 c1) 
    { 
     c1.Reference = 1234; 
     c1.Name = "John"; 
    } 

    static void InitializeCustomer(Customer2 c2) 
    { 
     c2.Name = "Mary"; 
     c2.Town = "Tokyo"; 
    } 
} 

class Customer1 
{ 
    public int Reference; 
    public string Name; 
} 

class Customer2 
{ 
    public string Name; 
    public string Town; 
} 

我想,以避免造成2“DoSomething的”方法,也避免了用不同的方法参数两次复制代码。我想用一个对象作为参数,但我需要在之后施放......你能指导我吗?

谢谢。

+0

+1。我不知道这在C#中是不允许的(它在C++中有效) – finnw 2011-02-03 19:11:20

回答

2

由于Customer1Customer2不共享通用接口,所以这是不可能的。

但是,您可以重新编写它,以便它们从基类(或接口)派生,并执行它们自己的初始化。这也会更清洁,因为它允许每个Customer自行初始化,这可以让问题保持更清晰的分离。

例如:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Customer1 c1 = new Customer1(); 
     DoSomething(c1); 

     Customer2 c2 = new Customer2(); 
     DoSomething(c2); 
    } 

    static void DoSomething<T>(T customer) where T : Customer 
    { 
     //... code here ... 
     customer.Initialize(); 
     //... code here ... 
    } 
} 

abstract class Customer 
{ 
    public abstract void Initialize(); 

} 
class Customer1 : Customer 
{ 
    public int Reference; 
    public string Name; 

    public override void Initialize() 
    { 
     this.Reference = 1234; 
     this.Name = "John"; 
    } 
} 

class Customer2 : Customer 
{ 
    public string Name; 
    public string Town; 

    public override void Initialize() 
    { 
     this.Name = "Mary"; 
     this.Town = "Tokyo"; 
    } 
} 
+0

我用你的方法@ReedCopsey。它工作正常。谢谢! – Dan 2011-02-04 12:28:59

2

您的Customer1Customer2应该从普通AbstractCustomer类或ICustomer接口继承。

这将允许您使用方法,说明如何处理,也消除了对仿制药的需求:

static void DoSomething(ICustomer customer) 
{ 
    //... code here ... 
    InitializeCustomer(customer); 
    //... code here ... 
} 

static void InitializeCustomer(ICustomer c) 
{ 
    c.Reference = 1234; 
    c.Name = "John"; 
} 

由于@Reed Copsey指出,这种解决方案假定这两种类型具有相同的成员。

如果您为两者(它是方法签名的条款)提供了相同的初始化方法,则可以分别初始化它们。

+0

虽然它们没有相同的成员,但这会很棘手。更好地移动初始化到客户基类,除非你可以使基类包含相同的API ... – 2011-02-03 18:21:01

0

你需要有一个单独的类实例来做到这一点。

一个更好的方法是使用继承,一旦客户从另一个继承,或者更好,它们都从一个公共基础继承。然后,您可以通过调用适当的方法在几乎任何地方初始化例程,并调用正确的版本。

0

也许共享接口?

interface ICustomer 
{ 
    void Initialize(); 
} 

class Program 
{ 

    static void Main(string[] args) 
    { 
     Customer1 c1 = new Customer1(); 
     DoSomething(c1); 

     Customer2 c2 = new Customer2(); 
     DoSomething(c2); 
    } 

    static void DoSomething<T>(T customer) where T : ICustomer 
    { 
     customer.Initialize(); 
    } 
} 

class Customer1 : ICustomer 
{ 
    public void Initialize() 
    { 
     Reference = 1234; 
     Name = "John"; 
    } 

    public int Reference; 
    public string Name; 
} 

class Customer2 : ICustomer 
{ 
    public void Initialize() 
    { 
     Name = "Mary"; 
     Town = "Tokyo"; 
    } 

    public string Name; 
    public string Town; 
}