can we access a private variable using an object

前端 未结 7 1882
一整个雨季
一整个雨季 2020-12-11 08:07

We cannot access a private variable of a class from an object, which is created outside the class, but it is possible to access when the same object is created inside the cl

7条回答
  •  不知归路
    2020-12-11 08:46

    why??

    It's true by the language specification. The access modifier private has the semantics that only the class or struct declaring a member is allowed to access that member.

    I suggest reading the specification for details. In particular, check out

    §3.5.1 Declared Accessibility

    §3.5.4 Accessibility constraints

    §10.2.3 Access Modifiers

    §10.2.6.2 Declared Accessibility

    §10.2.6.5 Access to private and protected members of the containing type

提交回复
热议问题