2011-02-03 55 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; 
}