2017-08-21 74 views
-3

我有三個類,即「Class1」,「Class2」和「Class3」以及一個接口IBase。設計建議 - 具有屬性,接口(或摘要)的類 - 接入問題

interface IBase 
{ 
    + PrpCommon {get;set;} 
} 

Class1:IBase 
{ 
+ PrpCommon {get;set;} 

+ Class1Prp {get;set;} (and few other properties) 
} 

Class2:IBase 
{ 
+ PrpCommon {get;set;} 
+ Class2Prp {get;set;} (and few other properties) 
} 

Class3:IBase 
{ 
    + PrpCommon {get;set;} 
    + Class3Prp {get;set;} } 

AccessClass 
{ 

    AccessFunction(IBase) 
{ 

    return IBase.PrpCommon;  
// here i need to access the other properties like Class3Prpr or Class2Prp or Class1Prp. 
} 
} 

MainClass 
{ 
    AccessClass.AccessFunction(new Class1) 
// Here need to access the other properties like Class3Prpr or Class2Prp or Class1Prp. 
} 

是否有可能???我聽說通過一些構造函數,可以訪問它,但不通過接口...任何想法。


回答

0

我不知道我得到這個正確。你想訪問AccessClass中派生類的屬性嗎? 在這種情況下,一種選擇是擁有AccessClass的基類或接口,每個IBase類和AccessClassFactory的特定實現。這樣的事情:

interface IAccessClass 
{ 
    AccessFunction<T>(T item) where T : IBase; 
} 

class AccessClass1 : IAccessClass<Class1>{ 
    AccessFunction(Class1 item){ 
     // here you can do whatever you want with the properties 
    } 
} 

class AccessClassFactory{ 
    IAccessClass<T> Create<T>() where T : IBase{ 
     var classType = typeof(T); 
     if(classType == typeof(Class1)) return new AccessClass1(); 
     // and so on 
    } 
} 

我希望你明白了!

+0

謝謝,我期待這樣......再次感謝你...... :) – kamal