内部抽象方法.为什么会有人拥有它们? [英] internal abstract methods. Why would anyone have them?

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

问题描述

我今天在做一些代码审查,发现了一些开发人员编写的旧代码.事情是这样的

I was doing some code review today and came across an old code written by some developer. It goes something like this

public abstract class BaseControl
{
    internal abstract void DoSomething();
}

如果你在同一个程序集中有一个派生类,它会工作

If you have a derived class within the same assembly, it would work

public class DerivedControl : BaseControl
{
    internal override void DoSomething()
    {
    }
}

但是在不同的程序集中派生基类会导致编译时错误

But deriving the base class in a different assembly would give compile time error

DerivedControl does not implement inherited abstract member 'BaseControl.DoSomething()

这让我想到了.为什么会有人将方法声明为内部抽象?

That got me thinking. Why would anyone declare a method as internal abstract ?

推荐答案

最初的程序员希望为客户端代码提供派生控件.但是要防止客户端继承和弄乱虚方法.这不是一个坏主意,通常很容易通过覆盖一个方法并执行诸如忘记调用基类方法之类的事情来破坏基类.

The original programmer wanted to make a derived control available to client code. But prevent the client from inheriting and messing with the virtual method. That's not a bad idea, it is usually easy to break a base class by overriding a method and doing something like forgetting to call the base class method.

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

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