日期:2014-05-17  浏览次数:20688 次

当private、protected修饰class
书中描述修饰class访问权限有四个级别:public、internal、protected、private

做过一年C#,我见过public class ClassName{...},也见过internal class ClassName{...},但怎么就没见到private class ClassName{...} 和 protected class ClassName{...}呢?

我不明白用private和protected声明类是什么意思,代表什么意义?
class c#

------解决方案--------------------
引用:
Quote: 引用:

类里也可以定义类的
* Types declared in compilation units or namespaces can have public or internal declared accessibility and default to internal declared accessibility.
* Class members can have any of the five kinds of declared accessibility and default to private declared accessibility. (Note that a type declared as a member of a class can have any of the five kinds of declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.)

来自
3.5.1 Declared accessibility
http://msdn.microsoft.com/en-us/library/aa691127(v=vs.71).aspx

你说的嵌套类吧,那么就说这两个东西只有在类中声明嵌套类才有意义了?


是的,嵌套类。
不是嵌套类不允许使用private, protected和protected internal
会报错:Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal