基本上我有一个设置,我需要从派生类访问成员,同时仍然将代码驻留在基类中,例如
public class DerivedClass : BaseClass<DerivedClass>{
DerivedClass()
{
// Set the value to 'test' here
}
}
public class BaseClass<T> where T : class, new(){
public int test {get; set;}
BaseClass()
{
// Use the value that DerivedClass set for 'test'
}
}
如果答案非常明显,我道歉
答案 0 :(得分:1)
您可以直接访问测试。实施例
public class Derived : Test
{
public Derived()
{
_test = 5;
}
}
public class Test
{
public int _test;
public int GetTest()
{
return _test;
}
}
var obj = new Derived();
var test = obj.GetTest(); // returns 5