为什么我的公共类不能扩展内部类? [英] Why can't my public class extend an internal class?

查看:37
本文介绍了为什么我的公共类不能扩展内部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白.

如果基类是抽象的并且仅用于为程序集中定义的公共子类提供通用功能,为什么不将其声明为内部类?

If the base class is abstract and only intended to be used to provide common functionality to public subclasses defined in the assembly, why shouldn't it be declared internal?

我不希望抽象类对程序集外的代码可见.我不想让外部代码知道这件事.

I don't want the abstract class to be visible to code outside the assembly. I don't want external code to know about it.

推荐答案

通过从类继承,您可以通过子类公开基类的功能.

By inheriting from a class, you expose the functionality of the base class through your child.

由于子类比其父类具有更高的可见性,您将暴露本应受到保护的成员.

Since the child class has higher visibility than its parent, you would be exposing members that would otherwise be protected.

您不能通过实现具有更高可见性的子类来违反父类的保护级别.

You can't violate the protection level of the parent class by implementing a child with higher visibility.

如果基类确实要由公共子类使用,那么您也需要将父类设为公共.

If the base class is really meant to be used by public child classes, then you need to make the parent public as well.

另一种选择是将您的父"保持在内部,使其非抽象,并使用它来组合您的子类,并使用接口强制类实现功能:

The other option is to keep your "parent" internal, make it non-abstract, and use it to compose your child classes, and use an Interface to force classes to implement the functionality:

public interface ISomething
{
    void HelloWorld();
}

internal class OldParent : ISomething
{
    public void HelloWorld(){ Console.WriteLine("Hello World!"); }
}

public class OldChild : ISomething
{
    OldParent _oldParent = new OldParent();

    public void HelloWorld() { _oldParent.HelloWorld(); }
}

这篇关于为什么我的公共类不能扩展内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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