我相信您的问题与良好的编程实践和there could be multiple solution or design technique for this question
.
我会创建这样的类 -
// create interface for providing {x} features
public interface IBObj_Sf
{
...
...
}
// create class which provides {x} features of IBObj_Sf interface
public class BObj_Sf : IBObj_Sf
{
...
...
}
// now implement BObj_Sub class like -
public class BObj_Sub
{
// mark as readonly depending it is modifiable or
// not withing BObj_Sub's lifecycle
private readonly IBObj_Sf _Sn;
public BObj_Sub(IBObj_Sf sn)
{
_Sn = sn;
}
public BObj_Sf Sn
{
get { return _Sn; }
private set { _Sn = value; }
}
}
用法:
BObj_Sf sf = new BObj_Sf();
BObj_Sub sub = new BObj_Sub(sf);
优点:明天如果你想换BObj_Sf
一个新的班级BObj_Xf
,那么只有以下几行会随着你的新班级而改变。-
BObj_Xf xf = new BObj_Xf();
BObj_Sub sub = new BObj_Sub(xf);
中的代码BObj_Sub
将保持不变。