您是否使用 Java 中的默认(包)可见性? [英] Do you use the default (package) visibility in Java?

查看:28
本文介绍了您是否使用 Java 中的默认(包)可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java 中的默认可见性意味着只有同一个 java 包中的其他类才能访问它.我当然看到它的使用,并且已经在多个场合使用过.

Default visibility in java means only other classes in the same java package can access it. I certainly see the use of it, and have used it in several occasions.

另一方面,我遇到的 90% 的默认可见性只是来自忘记添加任何可见性关键字的开发人员.

On the other hand, 90% of the default visibility I encounter is simply from a developer that forgot to add any visibility keyword.

因此,一方面,有合法用途,另一方面,它通常会掩盖不良代码.您在开发团队中做什么?

So, on the one hand, there's legitimate uses, on the other, it often masks bad code. What do you do in your development teams?

推荐答案

我通常会尝试让每个人从最有限的范围开始,只有在我们认为设计中有必要时才开始向外扩展.有时,我们决定从 private 迁移到 package-private 也是因为我们需要编写测试而不必走使用模拟框架的路线(参见 Powermock).

I generally try to get everyone to start with the most limited scope and only start to expand outward if we see that it's necessary in the design. Occasionally the decision to move from private to package-private is also driven by our need to write tests without having to go down the route of using a mocking framework (see Powermock).

除此之外,我们对可变性也遵循相同的心态.一切都从 final 开始,只有当我们看到需要修改状态时,我们才会删除它.

To add to this, we follow the same mentality on mutability. Everything starts out as final and only as we see the need to modify state do we remove it.

这篇关于您是否使用 Java 中的默认(包)可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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