Java Nested class

本文介绍了Java中的嵌套类(Nested class),并探讨了其在逻辑分组、增强封装性和提高代码可读性及可维护性方面的作用。通过具体实例说明了如何利用嵌套类来实现更高效的设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     本人在看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.

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值