大学期间我一直在使用public,想知道public, private和protected之间的区别吗?
还有,相对于什么都没有,静态有什么作用呢?
大学期间我一直在使用public,想知道public, private和protected之间的区别吗?
还有,相对于什么都没有,静态有什么作用呢?
当前回答
Private状态表示变量只能被同一类的对象访问。受保护状态将这种访问权限扩展到该类的后代。
“从上表中我们可以看到隐私和受保护之间的区别……我认为两者是一样的....那么这两个单独的命令有什么用呢?
查看MSDN链接了解更多信息
其他回答
public means that it can be accessed by any class in any assembly, which includes the class itself. protected internal means it can be accessed by the class itself (in the class definition), and it can be accessed by any class in the current assembly, but outside of the assembly it can only be accessed by a class that inherits the class, or by the class itself (if it is a partial class) – basically it means internal inside the assembly and protected outside of the assembly. protected means it can only be accessed by the class itself, or accessed by a class that inherits it and that class can be in any assembly internal means it can be accessed by the class itself or by any class in the assembly but can't be accessed at all outside of the assembly unless by the class itself (i.e. it is a partial class) private protected means it can only be accessed by the class itself, or it can be accessed by a class that inherits it and only if that class is in the current assembly. Outside of the assembly, it can only be accessed by the class itself (i.e. it is a partial class) – basically combines internal and protected, or another way of putting it is it's private outside of the assembly and protected inside the assembly. private means that it can only be accessed by the class itself no access modifier: The default access for everything in C# is "the most restricted access you could declare for that member"., which is private for a member / method / nested class in a class and internal for a non-nested class.
In the above text 'accessed' means accessed via an object of the class type, which within a method of the class itself will be the implicit this object, or perhaps the method instantiates an explicit object of the current class type and accesses it through that object. Both are considered to be being accessed by the class itself and therefore the access rules are the same. This also applies to an access being performed from a static method, or when it is a static member / method being accessed, except the access is performed using the class scope and not and object. A member / method of a static class needs to be explicitly made static or it won't compile.
没有嵌套的类可以是公共的,也可以是内部的,默认情况下是内部的。嵌套的类可以是任何访问类型,如果父类是静态的,它就不需要是静态的,它的成员也不需要是静态的。内部类意味着只能实例化它或从当前程序集访问它的静态成员。
您可以在内部或私有嵌套类中拥有公共成员/方法/嵌套类——只有访问说明符(在正在进行的访问的完全限定名称中)低于当前正在进行的访问所需的级别才能阻止访问。
Inheritance in C# is always public unlike C++, which can privately or protectedly inherit, which then changes the access of all classes that then inherit from the class that is inheriting from this class, as well as the access via object / via class scope of type of the class that inheriting privately / protectedly from the class and the class that is inheriting from the class that is inheriting privately / protectedly form the class, and so on. The access is changed such that all access modifiers less restrictive than private or protected are made private and protected respectively.
小心!注意类的可访问性。默认情况下,所有人都可以访问公共和受保护的类和方法。
此外,微软在显示访问修饰符(公共的,受保护的,等等。关键字)当Visual Studio中创建新类时。因此,请仔细考虑类的可访问性,因为它是通往实现内部的大门。
我认为这与好的面向对象设计有关。如果您是库的开发人员,则希望隐藏库的内部工作方式。这样,以后就可以修改库的内部工作方式。所以你把你的成员和辅助方法设为私有,只有接口方法是公共的。应该被覆盖的方法应该受到保护。
这些访问修饰符指定成员可见的位置。你应该读读这个。以IainMH给出的链接为起点。
静态成员是每个类一个,而不是每个实例一个。
c#总共有6个访问修饰符:
private:使用此可访问性声明的成员可以在包含类型中可见,但对任何派生类型、同一程序集中的其他类型或包含程序集中之外的类型都不可见。也就是说,访问仅限于包含类型。
protected:使用此可访问性声明的成员可以在包含程序集中从包含类型派生的类型中可见,也可以在包含程序集中从包含类型派生的类型中可见。也就是说,只能访问包含类型的派生类型。
internal:使用此可访问性声明的成员可以在包含该成员的程序集内可见,它对包含该成员的程序集之外的任何程序集都不可见。也就是说,访问仅限于包含程序集。
internal protected:使用此可访问性声明的成员可以在包含程序集内部或外部从包含类型派生的类型中可见,它也对包含程序集内的任何类型可见。也就是说,访问仅限于包含程序集或派生类型。
public:使用此可访问性声明的成员可以在包含此成员的程序集或引用包含该成员的程序集的任何其他程序集中可见。也就是说,访问不受限制。
在c# 7.2中,增加了一个新的可访问性级别:
private protected:使用此可访问性声明的成员可以在包含程序集中从此包含类型派生的类型中可见。对于非从包含类型派生的任何类型或包含程序集外部的任何类型都不可见。也就是说,访问权限仅限于包含程序集中的派生类型。
源代码,包括新的私有受保护访问修饰符的示例代码