内部阶级的用法 [英] Usage of inner class

查看:85
本文介绍了内部阶级的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以理解什么是内部类以及如何编写程序.我的问题是程序员在什么情况下真正需要内部类?

I can understand what inner class is and how to write program. My question is in what situation do programmers really need inner class?

推荐答案

有时,某些功能最好用一个对象来表示,但是仅在另一个对象的上下文中才有意义,而不必公开外部环境,并且可以从访问父类数据中受益(以免违反封装).

Sometimes there is some functionality which is best represented as an object, but which is only meaningful within the context of another object, which does not necessarily need to be exposed to the outside world, and which can benefit from having access to the parent classes data (so as to not violate encapsulation).

我能想到的最好的例子是将Node类放入LinkedList中.节点仅对LinkedList有意义,因此它们仅存在于一个节点中. LinkedList之外的任何人都不会关心节点或应该访问它们.

The best example that I can think of is putting a Node class inside of a LinkedList. Nodes are only meaningful to the LinkedList, so they only exist within one. No one outside of the LinkedList cares about nodes or should have access to them.

这篇关于内部阶级的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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