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