class MainClass {
private int someMethod() {
IList <SubClass> obj = db.Query <SubClass> (delegate(SubClass obj) {
return obj.Points == 100;
});
}
}
class SubClass : MainClass {
public int someField;
}
SubClass obj = new SubClass();
obj.someMethod();
SubClasses可能有所不同,我需要获取此实例才能对db4o进行查询。
答案 0 :(得分:0)
从基类中评估子类类型以进行一些处理,通常是一个错误的决定,考虑将一个组合的方法添加到基类,然后利用专门的处理到它的子类:
class MainClass {
public string ComposedMethod(){
//Base class processing...
var retVal = someMethod();
//Even more Base class processing...
return retVal;
}
public virtual string someMethod(){
}
}
class SubClass : MainClass {
public override string someMethod(){
return this.GetType().ToString(); //Or whatever you need to do
}
}
用法:
SubClass obj = new SubClass();
obj.ComposedMethod();
答案 1 :(得分:-1)
class MainClass
{
public string someMethod()
{
Type type = this.GetType(); //SubClass
return "";
}
}
class SubClass : MainClass
{
public int someField;
}
它是一个黑客,但不推荐,因为它不符合多态的目的。