문제

I have a base abstract class BasePerson. I have another abstract class BaseStudent and this inherits from BasePerson. Here is the example.

public abstract class BasePerson
{
    public string Name{get;set;}
    public string LastName{get;set;}
    public abstract object this[string propertyName]{get;set;}
}

public abstract class BaseStudent : BasePerson
{
    public string Test{get;set;}
    //this class inherits from BasePerson and it forces to override the indexer.
    //I can do this:
    public override object this[string propertyName]
    {
        get{return null;} 
        set
        {
            //do stuff;
        }
    }
}

public class Student : StudentBase
{
    //other properties
}

Now I am not able to force Student class to override the indexer. What should I do to force Student to override the indexer? I can't remove the indexer from BasePerson class.

Help appreciated!

도움이 되었습니까?

해결책

If you want to force it, don't implement it on BaseStudent. Since BaseStudent is abstract, it does not need to implement all the abstract members from BasePerson.

public abstract class BasePerson
{
    public string Name{get;set;}
    public string LastName{get;set;}
    public abstract object this[string propertyName]{get;set;}
}

public abstract class BaseStudent : BasePerson
{
    public string Test{get;set;}
}

public class Student : BaseStudent
{
    //must implement it here since Student isn't abstract!
}

abstract classes don't need to define all the abstract members of their inherited class, so you can feel free to pass up the responsibility to whatever concrete class would implement it. Student isn't defined to be abstract so it must implement whatever members have not already been implemented by the chain of base classes that it has inherited.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top