Why friend directive is missing in Java?

后端 未结 8 1430
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 14:23

I was wondering why Java has been designed without the frienddirective that is available in C++ to allow finer control over which methods and instance variables

8条回答
  •  温柔的废话
    2020-12-03 14:40

    In addition to the aforementioned package visibility, Java also offers inner and anonymous classes which are not only friends by default, but also automatically have a reference to the containing class. Since creating such helper classes is probably the only reasonable way to use friend in C++, Java doesn't need it since it has another mechanism for that. Iterators are a very good example of this.

提交回复
热议问题