简体   繁体   English

抽象类继承另一个抽象类问题

[英]an abstract class inherits another abstract class issue

I have an inheritance schema like below: 我有一个继承架构,如下所示:

public abstract class BaseAttachment
{
    public abstract string GetName();
}

public abstract class BaseFileAttachment:BaseAttachment
{
    public abstract string GetName();
}

public class ExchangeFileAttachment:BaseFileAttachment
{
    string name;
    public override string GetName()
    {
        return name;
    }
}

I basically want to call GetName() method of the ExchangeFileAttachment class; 我基本上想调用ExchangeFileAttachment类的GetName()方法; However, the above declaration is wrong. 但是,上述声明是错误的。 Any help with this is appreciated. 对此有任何帮助表示赞赏。 Thanks 谢谢

The two immediate problems I see is that your final ExchangeFileAttachment class is declared abstract , so you'll never be able to instantiate it. 我看到的两个直接问题是你的最终ExchangeFileAttachment类被声明为abstract ,所以你永远无法实例化它。 Unless you have another level of inheritance you are not showing us, calling it will not be possible - there's no way to access it. 除非你有另一级别的继承,否则你不会向我们展示,所以无法调用它 - 没有办法访问它。 The other problem is that BaseFileAttachment has a property that is hiding the GetName() in BaseAttachment . 另一个问题是BaseFileAttachment有一个隐藏BaseAttachment GetName()BaseAttachment In the structure you are showing us, it is redundant and can be omitted. 在您向我们展示的结构中,它是多余的,可以省略。 So, the 'corrected' code would look more like: 因此,“更正”的代码看起来更像是:

public abstract class BaseAttachment
{
    public abstract string GetName();
}

public abstract class BaseFileAttachment : BaseAttachment
{
}

public class ExchangeFileAttachment : BaseFileAttachment
{
    string name;
    public override string GetName()
    {
        return name;
    }
}

I put corrected in quotes because this use-case still does not make a ton of sense so I'm hoping you can give more information, or this makes a lot more sense on your end. 我在引号中加了修正,因为这个用例仍然没有多大意义,所以我希望你能提供更多的信息,或者这在你的结尾更有意义。

Just remove the redeclaration from BaseFileAttachment : 只需从BaseFileAttachment删除重新声明:

public abstract class BaseFileAttachment : BaseAttachment
{
}

BaseFileAttachment already inherits the abstract GetName declaration from BaseAttachment . BaseFileAttachment已经从BaseAttachment继承了抽象GetName声明。 If you really want to mention it again in BaseFileAttachment , use the override keyword: 如果您真的想在BaseFileAttachment再次提及它,请使用override关键字:

public abstract class BaseFileAttachment : BaseAttachment
{
    public override abstract string GetName(); // that's fine as well
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM