0
如何知道子類重寫其父類的方法?目前,我正在使用布爾標誌,它在父類上設置爲false,並且當一個孩子重寫它時,孩子必須設置標誌。在它正在工作的時候,我想知道這個問題是否有更乾淨的解決方案。知道父類沒有附加標誌的子類重寫方法
// The parent class
public Class_A
{
protected bool _hasCheckData = false;
public bool HasCheckData
{
get { return _hasCheckData; }
}
public abstract bool CheckData(File fileToCheck)
{
return true;
}
}
// Lot's of children from class A, this is one of them
public Class_B : Class_A
{
public override bool CheckData(File fileToCheck)
{
// the following line will be duplicated for all Class_A's children
// who implemented the checking of the file. How to avoid this?
_hasCheckData = true;
// checking the file
// and return the result
}
}
public Class_C
{
public void Test(File fileToCheck)
{
Class_B fileAbcChecker = new Class_B();
if (fileAbcChecker.HasCheckData)
fileAbcChecker.CheckData(fileToCheck);
}
}
小提示:Class_A必須做一些事情,即返回false(或true) – David 2014-10-16 06:37:42