为什么一个抽象类实现的抽象基类的抽象方法? [英] Why should an abstract class implement an abstract method of an abstract base class?

查看:116
本文介绍了为什么一个抽象类实现的抽象基类的抽象方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中,类派生实现抽象方法方法从类主要。但是,我想不出一个理由来填补方法体中的抽象派生类的实现。当然,我只是要真正的类中实现的抽象方法。

那么如何避免这样做呢?我还能做什么?

 抽象类主要
{
    公共抽象无效的方法();
}
抽象类派生:主
{
    公共覆盖无效的方法()
    {
    }
}

类RealClass:派生
{

}
 

解决方案

通常,如果有人已经指定一个抽象类有一个抽象的方法,它可能是因为该类依赖于该方法对一些是做什么的,或者是因为这是一个预期的API,那就没有意义的父类实现在这个时候的一部分。在这两种情况下,存在的必须的是执行,一旦你得到一个非抽象实现类。

还要注意的是,如果你正在实施的接口的,您需要声明如何该接口将被执行,即使你只是调用成员抽象,并通过责任推给子类。

 公共接口IPET {字符串GetNoise();诠释CountLegs();虚空城();}
公共抽象类宠物:IPET
{
    公共字符串名称{;组;}
    公共抽象的字符串GetNoise(); //这些都必须在这里
    公共抽象INT CountLegs();
    公共抽象无效城();
}
 

当谈到实现子类,则必须视情况而定了一些选择。如果你的实现是一个抽象类,你不应该需要实现的抽象方法。

 公共抽象类的四足动物:宠物
{
    公众覆盖INT CountLegs(){返回4; }
}
 

如果您的实现是不可抽象的,但标准的原因,有问题的方法其实并不在你的情况下适用,你可以做一个无操作方法(在方法无效的情况下),或者返回一些虚拟值,甚至抛出一个NotImplementedException,以表明该方法不应该被称为摆在首位。

 公共类鱼:宠物
{
    公众覆盖字符串GetNoise(){返回;} //虚值:鱼不制造噪音
    公众覆盖INT CountLegs(){返回0;}
    公众覆盖无效城(){} //空操作
    //公共覆盖无效城(){抛出新的NotImplementedException(鱼不能走); }
}
 

这是否回答你的问题?

In the following example, the class Derived implements the abstract method method from class Main. But I can't think of a reason to fill in the method body in the abstract Derived class' implementation. Surely I should only implement abstract methods within real classes.

So how can I avoid doing it? What else can I do?

abstract class Main
{
    public abstract void method();
}
abstract class Derived : Main
{
    public override void method()
    { 
    }
}

class RealClass : Derived
{

}

解决方案

Usually if someone has specified that an abstract class has an abstract method, it's either because that class depends on that method for some of what it does, or it's because it's part of an expected API that it wouldn't make sense for the parent class to implement at this time. In either case, there must be an implementation once you get to a non-abstract implementation of the class.

Note also that if you are implementing an interface, you are required to state how that interface will be implemented, even if you just call the member abstract and pass the responsibility onto the subclass

public interface IPet {string GetNoise(); int CountLegs(); void Walk();}
public abstract class Pet : IPet
{
    public string Name {get; set;}
    public abstract string GetNoise(); // These must be here
    public abstract int CountLegs();
    public abstract void Walk();
}

When it comes to implementing the sub-class, you have a few choices depending on the circumstances. If your implementation is itself an abstract class, you shouldn't need to implement the abstract method.

public abstract class Quadruped : Pet
{
    public override int CountLegs () { return 4; }
}

If your implementation is non-abstract, but the standard reason for the method in question really doesn't apply in your circumstance, you can do a no-op method (in the case of void methods), or return some dummy value, or even throw a NotImplementedException to indicate that the method should never have been called in the first place.

public class Fish : Pet
{
    public override string GetNoise() {return "";} // dummy value: fish don't make noise
    public override int CountLegs() {return 0;}
    public override void Walk() {} // No-op
    // public override void Walk() { throw new NotImplementedException("Fish can't walk"); }
}

Does that answer your question?

这篇关于为什么一个抽象类实现的抽象基类的抽象方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆