什么样的访问修饰符可以应用于类? [英] What kind of access modifiers can be applied to a class?

查看:146
本文介绍了什么样的访问修饰符可以应用于类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个小小的研究之后,我已经确定,你可以申请类的唯一访问修饰符是:




  • 公开 - 提供任何组装

  • 内部 - 仅在当前汇编



但下面的错误信息似乎暗示,如果一个类的的,它可以被定义为一个命名空间中定义的私人保护受保护的内部



是公开的和内部的唯一类修饰符您可以使用类或是否有更多的

 使用系统?; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

命名空间Test2343434
{
类节目
{
静态无效的主要(字串[] args)
{
工具。 toolManager toolManager =新Tools.ToolManager();

到Console.ReadLine();
}
}

}

命名工具
{
//错误:在一个命名空间中定义的元素不能明确
//声明为private,protected或受保护的内部
类的私有ToolManager
{
公共ToolManager()
{
Console.WriteLine(内幕刀具管理);
}
}
}


解决方案

一个嵌套的类型可以被声明为private,protected或受保护的内部。您仍然可以声明嵌套类型为公共或内部当然 - 它只是你可以的只有的上述访问修饰符声明他们时,他们正在嵌套:

 公共类OuterClass 
{
私有类PrivateNested {}
保护类ProtectedNested {}
受保护的内部类ProtectedInternalNested {}
酒店的公共类PublicNested {}
内部类InternalNested {}
}

注意,你的不能的声明嵌套在类型结构保护受保护的内部,因为它没有任何意义,这样做的:)

 公共结构OuterStruct 
{
私有类PrivateNested {}
公共类PublicNested {}
内部类InternalNested {}
}

C#不允许嵌套在接口类型。(遗憾的是,IMO - 这将是代码合同有用)



这一切是其他嵌套类型(枚举,结构,接口,委托)太真。


After a little research I've determined that the only access modifiers which you can apply to classes are:

  • public - available in any assembly
  • internal - available only in the current assembly

but the error message below seems to imply that if a class is not defined in a namespace that it could be defined as private, protected, or protected internal.

Are public and internal the only class modifiers you can use on class or are there more?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test2343434
{
    class Program
    {
        static void Main(string[] args)
        {
            Tools.ToolManager toolManager = new Tools.ToolManager();

            Console.ReadLine();
        }
    }

}

namespace Tools
{
    //error: Elements defined in a namespace cannot be explicitly 
    //declared as private, protected, or protected internal
    private class ToolManager 
    {
        public ToolManager()
        {
            Console.WriteLine("inside tool manager");
        }
    }
}

解决方案

A nested type can be declared private, protected, or protected internal. You can still declare nested types as public or internal of course - it's just that you can only declare them with the above access modifiers when they're nested:

public class OuterClass
{
    private class PrivateNested {}
    protected class ProtectedNested {}
    protected internal class ProtectedInternalNested {}
    public class PublicNested {}
    internal class InternalNested {}
}

Note that you can't declare a type nested in a struct to be protected or protected internal because it doesn't make any sense to do so :)

public struct OuterStruct
{
    private class PrivateNested {}
    public class PublicNested {}
    internal class InternalNested {}
}

C# doesn't allow types to be nested in interfaces (unfortunately, IMO - it would be useful for code contracts).

All of this is true for other nested types (enums, structs, interfaces, delegates) too.

这篇关于什么样的访问修饰符可以应用于类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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