Access private field of a instance object

后端 未结 5 1020
孤城傲影
孤城傲影 2021-01-13 22:38

I have a class, which has one field named orbits (it has the same type as my class Body and has the private modifier):



        
相关标签:
5条回答
  • 2021-01-13 22:48

    Because victim is an instance of Body, it can access any field of a Body isntance.

    0 讨论(0)
  • 2021-01-13 23:01

    Privacy is not per instance - it's per class.

    The class can access the private fields of all instances.

    For example, the method equals( Object o ) can cast o (if appropriate) to the same type, and compare its private members with the object on which equals() was called.

    0 讨论(0)
  • 2021-01-13 23:06

    Since victim is also of type Body, the any instance of Body can access the private members of the victim instance.

    0 讨论(0)
  • 2021-01-13 23:09

    victim is an instance of class Body and has all Attributes of that class. Every instance will have a private property orbits.

    If you need a class-Attribute use "private static"

    0 讨论(0)
  • 2021-01-13 23:11

    According to section 6.6.1 of the Java Language Specification:

    Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

    0 讨论(0)
提交回复
热议问题