我有了一個類,如下C#組件:委託 - 最佳實踐
namespace SharedComponent{
class TestResult {
//several members
}
}
在我引用這個組件另一個現有的C#應用程序,我需要實例化這個同一類,但與附加標識符如下。
namespace ClientApplication {
class TestResult
{
//exact same members as above including methods
//actually the shared component class was created by gleaming
//that from this application!
int PersonID; //additional identifier
//not suitable to have in the shared component
}
}
在客戶端應用程序中有幾種依賴附加標識符的方法。所以對我來說,仿效一個拷貝構造函數並創建這個對象並填充附加參數是非常誘人的。這樣我就可以使用現有的功能,只需對類進行最少的更改。
另一種方法可以是將其餘細節添加爲客戶端實現的引用。
namespace ClientApplication {
class TestResult {
SharedComponent.TestResult trshared = new SharedComponent.TestResult()
//but this warrants I have my class methods to delegate
//to the sharedcomponent throughout ; example below
internal bool IsFollowUp(ClientApplication.TestResult prevTest)
{
//a similar method is being used
//where a function takes the class object as parameter
trshared.IsFollowUp(prevTest.trshared);
}
int PersonID; //additional identifier
}
}
哪個選項更好?這方面的最佳做法是什麼?
環境:VS2008,C#,WINXP/Win7的
客戶端應用程序類是否可以從原始繼承? – sq33G 2011-12-19 23:30:15