[英]Inherit abstract class inside another abstract class with same function names
I have two abstract classes AbstractClass1
and AbstractClass2
that both should have the same method names Meth1
. 我有两个抽象类AbstractClass1
和AbstractClass2
,它们都应具有相同的方法名称Meth1
。
Now what if my AbstractClass1
inherits from AbstractClass2
. 现在,如果我的AbstractClass1
继承自AbstractClass2
该怎么办。 Currently it is throwing me a compile time error as "Hides Inherited Abstract Member"
. 目前,它正在向我抛出"Hides Inherited Abstract Member"
的编译时错误。 Below is my code. 下面是我的代码。
abstract class AbstractClass1 : AbstractClass2
{
public abstract string Meth1();
}
abstract class AbstractClass2
{
public abstract string Meth1();
}
You can use abstract override
on derived class method to avoid compile time error. 您可以对派生类方法使用abstract override
,以避免编译时错误。
abstract class BaseAbstract
{
public abstract string Meth1();
}
abstract class ChildAbstract : BaseAbstract
{
public abstract override string Meth1();
}
BUT please note that this is not quite right place to use abstract override
. 但是 请注意,这不是使用abstract override
正确位置。 Usually abstract override is used when deriving an abstract class from non abstract class. 从非抽象类派生抽象类时,通常使用抽象覆盖。 to ensure that derived class implements the method. 确保派生类实现该方法。 (see this ) (参见此 )
Take a look at below code 看下面的代码
class BaseNonAbstract
{
public virtual string Meth1()
{
// do something
return string.Empty;
}
}
abstract class ChildAbstract : BaseNonAbstract
{
public abstract override string Meth1();
}
Try this 尝试这个
abstract class AbstractClass1 : AbstractClass2
{
}
abstract class AbstractClass2
{
public abstract string Meth1();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.