为什么“受保护”? Java中的修饰符允许访问同一个包中的其他类? [英] Why does the "protected" modifier in Java allow access to other classes in same package?

查看:193
本文介绍了为什么“受保护”? Java中的修饰符允许访问同一个包中的其他类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,具有protected修饰符的成员不仅可以由同一个类和子类访问,还可以由同一个包中的每个人访问,这是什么原因?

What is the reason that in Java, a member with a "protected" modifier can not only be accessed by the same class and by subclasses, but also by everyone in the same package?

我想知道语言设计的原因,而不是实际的应用程序(例如,测试)

I am wondering about language design reasons, not actual applications (e.g., testing)

推荐答案

这种设计基于这样的想法:包是适当的单位,由一个内部一致的团队维护和发布;继承关系与谁维持和释放什么时间的关系要少得多。

This design is based on the idea that the package is the appropriate unit, maintained and released by one internally consistent team; inheritance relationships have much less to do with who's maintaining and releasing what when.

这篇关于为什么“受保护”? Java中的修饰符允许访问同一个包中的其他类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆