我们可以将密封类作为私人密码吗? [英] Can we make sealed class as private?

查看:55
本文介绍了我们可以将密封类作为私人密码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以将密封的课程设为私人课程吗?如果我们可以这样做,那么它的用途是什么?



我尝试了什么:



我们可以将密封的类作为私有类,如果我们可以这样做那么它的用途是什么?

解决方案

取决于:你不能直接在命名空间中声明它:

  namespace  MyNamespace 
{
private 密封 class MySealedClass
{
}
}

您将收到错误消息:

命名空间中定义的元素不能显式声明为私有,受保护或受保护的内部



但是你可以在一个包含的类中:

  public   class  MyContainerClass 
{
私有 密封 MySealedClass
{
}
}

它的行为完全符合您的预期:该类仅在包含的类中可用,并且不能继承或以其他方式扩展。


Can we make the sealed class as private and if we can do so then what is the use of it?

What I have tried:

Can we make the sealed class as private and if we can do so then what is the use of it?

解决方案

Depends: you can't declare it directly within the namespace:

namespace MyNamespace
    {
    private sealed class MySealedClass
        {
        }
    }

You will get an error:

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal


But you can within a containing class:

public class MyContainerClass
    {
    private sealed class MySealedClass
        {
        }
    }

And it behaves exactly as you would expect: the class is only usable within the containing class, and cannot be inherited or otherwise expanded.


这篇关于我们可以将密封类作为私人密码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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