是否需要将私有嵌套类的内部声明为私有? [英] Is it needed to declare innards of private nested classes private?

查看:200
本文介绍了是否需要将私有嵌套类的内部声明为私有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

私人之后,私人的1000年之后,我认为可能不需要

After 1000s of privates in private it occurred to me that it may not be needed

public class Outer {

    private static class Inner { // you may drop static
        private void innerMethod() {}
    }
}

innerMethod()中删除​​ private 会使封装 (或使用,例如由Outer)?也想想反思

如果是建议放弃它还是保持它的编码风格?

Is there any case that dropping private from innerMethod() would make a difference in encapsulation (or use, by Outer for instance) ? Think also reflection
If not is it recommended to drop it or keep it vis a vis coding style ?

编辑:只是意识到, I 的做法肯定是错误的 - 至少 Inner 字段 - 声明这些字段private然后在外部类中使用它们,因为这会在字节码中生成(synthetic)访问器 - 至少是膨胀。很好 - 现在我更感兴趣的是有一个帐户声明这些( Inner 的字段,在中使用的方法的安全影响外部)package private(或public @JBNizet在评论中说的)

EDIT : just realized that the way I do it is certainly wrong - at least for Inner's fields - declaring those fields private and then using them in the outer class - as this generates ("synthetic") accessors in the bytecode - which is at least bloat. Great - so now I am even more interested to have an account on the security implications of declaring those (Inner's fields, methods used in Outer) package private (or public as @JBNizet says in the comments)

推荐答案

你目前使用内部类。

我对内部类的哲学是减少重构的负担。我保持内部类的封装: private 内部类的方法和字段不是从外部类访问的,即使他们可以。内部类的点,然后,是将其范围减少到仅包含类。这样,如果稍后一个内部类可以在其他地方重用,它几乎不需要工作(和非 - static 内部类的工作量)移动到它的

My philosophy for inner classes is to reduce the burden of refactoring. I maintain encapsulation of inner classes: private methods and fields of the inner class are not accessed from the outer class even though they can be. The point of inner classes, then, is to reduce its scope to only the containing class. That way, if later an inner class can be reused elsewhere, it requires almost no work (and a trivial amount of work for non-static inner classes) to move it into its own file.

无论上述是否是您的风格,都会影响删除或保留私人

Whether or not the above is your style will affect the following reasons for dropping or keeping the private around methods/fields.

删除 private 的原因是:


  • 外部类可以访问私有成员和内部类的方法,这意味着这些字段/方法不会真正封装
  • b
  • 减少键入

拒绝 private


  • 将内部类 private 的方法用作文档:外部类不应使用这些方法

  • 如果保留 private 内部类到其自己的文件
  • 如果删除 private ,则有 public 内部类和 private 内部类:更多为程序员考虑

  • 如果 private > ,所有能访问外部文件的人都可以访问内部类的私有数据

  • Making the methods of an inner classes private serves as documentation: the outer class shouldn't use these methods
  • If private is kept, it makes it much easier to promote an inner class to its own file
  • If private is dropped, there are two styles for public inner classes and private inner classes: more for the programmer to think about
  • If private is dropped and the inner class was made public, suddenly everyone who has access to the outer file has access to the inner class's private data

鉴于上述风格,我认为防止丢弃的情况更强。

Given the above style, I think the case against dropping is stronger.

这篇关于是否需要将私有嵌套类的内部声明为私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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