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

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

问题描述

我真的不明白这一点。

如果基类是抽象的,只用于被用来提供在程序集中定义的公共子类的通用功能,为什么不应该是被宣布为内部?

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?

我不希望抽象类是装配外code可见。我不希望外界code,以了解它。

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天全站免登陆