[英]Overriding abstract members of abstract classes
I have a base abstract class BasePerson
. 我有一个基本的抽象类BasePerson
。 I have another abstract class BaseStudent
and this inherits from BasePerson
. 我有另一个抽象类BaseStudent
,它继承自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. 现在我无法强制Student
类覆盖索引器。 What should I do to force Student to override the indexer? 我应该怎么做才能强制学生覆盖索引器? I can't remove the indexer from BasePerson
class. 我无法从BasePerson
类中删除索引器。
Help appreciated! 帮助赞赏!
If you want to force it, don't implement it on BaseStudent
. 如果要强制它,请不要在BaseStudent
上实现它。 Since BaseStudent
is abstract
, it does not need to implement all the abstract
members from BasePerson
. 由于BaseStudent
是abstract
,因此不需要实现BasePerson
所有abstract
成员。
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. abstract
类不需要定义其继承类的所有 abstract
成员,因此您可以随意将责任传递给任何具体的类来实现它。 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. Student
没有被定义为abstract
因此它必须实现它已经继承的基类链尚未实现的任何成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.