Java Nested class

     本人在看Android文档的时候发现有个Java语法还挺有意思的,就是Java Nested class,查看了sun的文档,原来是类里面的嵌套类。http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html


本人介绍一篇文章给大家看看:http://www.cn-java.com/www1/bbs/redirect.php?tid=13595&goto=lastpost&sid=D7zYoM


为什么要使用 Nested class???官方的解释如下

1、类在逻辑山的分组

2、增加封装性

3、代码可以更好地维护

Why Use Nested Classes?

There are several compelling reasons for using nested classes, among them:

  • It is a way of logically grouping classes that are only used in one place.
  • It increases encapsulation.
  • Nested classes can lead to more readable and maintainable code.

Logical grouping of classes —If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

Increased encapsulation —Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private . By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

More readable, maintainable code —Nesting small classes within top-level classes places the code closer to where it is used.

 

 

 

 

 

 

你可能感兴趣的:(java,android,PHP,Access,sun)