为什么我重写抽象抽象方法? [英] Why can I abstract override an abstract method?

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

问题描述

我有一个抽象基类:

 抽象类Foo 
{
虚拟无效DoSomeStuff( )
{
//做一些东西
}

抽象无效DoSomeCrazyStuff();
}

和从派生另外一个抽象类:

 抽象类酒吧:富
{
抽象的覆盖无效DoSomeStuff();

抽象的覆盖无效DoSomeCrazyStuff();
}



我明白你为什么会想抽象覆盖 DoSomeStuff() - 这将需要进一步派生类的新的实现。但我不明白,为什么你想抽象覆盖 DoSomeCrazyStuff()。至于我可以告诉大家,它是多余的 - 我敢肯定,取出,将具有零负面影响。



是否存在这样一种抽象的抽象的覆盖做了一些用例一些有用的东西?如果没有,为什么没有一个编译器警告,通知我说什么,我已经写了什么都不做?


解决方案

为什么我重写抽象抽象方法?




对于初学者来说,有防止它没有实际的原因。如果它产生一个编译器错误,所有会做的是让班更脆。例如:

 抽象类Foo 
{
虚拟无效DoSomeStuff()
{
//做一些东西
}
}

抽象类酒吧:富
{
抽象的覆盖无效DoSomeStuff();
}

如果抽象抽象的覆盖是非法的,不断变化的DoSomeStuff上富抽象现在想防止从酒吧编译。抽象覆盖是多余的,但有没有潜在的负面影响,所以编译器是好的与此有关。







为什么没有一个编译器警告,通知我说什么,我已经写了什么都不做?




编译器生成警告表示危险某种东西:不明确的方法隐藏,无法访问的代码,使用过时的方法,等等。唯一的问题不必要的抽象的覆盖可以表明的是,代码不能有效地写入。这不是编译器关心。







有一些使用情况下抽象覆盖一个抽象的东西确实有用?




在功能上没有。但是,也有少数使用情况下,你可能会有意这样做的:




  • 要提高代码的可读性。具有冗余抽象的覆盖将作为一个提醒,方法是抽象的。

  • 如果将来基类的变化包括提供虚拟实现,你可以抢先阻止访问该基地某些类类。

  • 如果抽象的覆盖是多余的,因为基类是从一个虚拟实现,以抽象的变化,它可以安全地被单独留在家中。


I have an abstract base class:

abstract class Foo
{
    virtual void DoSomeStuff()
    {
        //Do Some Stuff
    }

    abstract void DoSomeCrazyStuff();
}

And another abstract class derived from that:

abstract class Bar : Foo
{
    abstract override void DoSomeStuff();

    abstract override void DoSomeCrazyStuff();
}

I understand why you'd want to abstract override DoSomeStuff() - it will require an new implementation for further derived classes. But I can't figure out why you would want to abstract override DoSomeCrazyStuff(). As far as I can tell, it's redundant - I'm pretty sure removing it would have zero negative impact.

Is there some use case where abstract override on an abstract does something useful? If not, why isn't there a compiler warning informing me that what I've wrote does nothing?

解决方案

Why can I abstract override an abstract method?

For starters, there's no practical reason for preventing it. If it produced a compiler error, all that would do is make classes more brittle. For example:

abstract class Foo
{
    virtual void DoSomeStuff()
    {
        //Do Some Stuff
    }
}

abstract class Bar : Foo
{
    abstract override void DoSomeStuff();
}

If abstract override on abstract was illegal, changing DoSomeStuff on Foo to abstract would now prevent Bar from compiling. The abstract override is redundant, but there's no potential negative side effects, so the compiler is okay with this.


Why isn't there a compiler warning informing me that what I've wrote does nothing?

The compiler produces warnings for certain things that represent risk: non-explicit method hiding, unreachable code, using obsolete methods, etc. The only "problem" an unnecessary abstract override could indicate is that the code was not efficiently written. That's not something the compiler cares about.


Is there some use case where abstract override on an abstract does something useful?

Not functionally. However, there are a few use cases where you might intentionally do so:

  • To improve the "readability" of the code. Having the redundant abstract override would serve as a reminder that the method is abstract.
  • If future changes to the base class include providing a virtual implementation, you can preemptively prevent some classes from accessing that base class.
  • If the abstract override is redundant because the base class was changed from a virtual implementation to abstract, it can safely be left alone.

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

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