日期:2014-05-20  浏览次数:20918 次

在实际使用中内部类一般干些啥?
如题,搜索了下坛子里内部类的帖子,没看出来在实际使用上内部类一般可用于常用的哪方面,有点郁闷,特请教下?

------解决方案--------------------
内部类的功能在于,每个内部类都能独立的继承一个接口的实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响。内部类使得多重继承的解决方案变得完整,并且内部类允许继承多个非接口类型(类或抽象类)
------解决方案--------------------
Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.

1.The inner class can have multiple instances, each with its own state information that is independent of the information in the outer class object.
2.In a single outer class you can have several inner classes, each of which implement the same interface or inherit from the same class in a different way. 3.The point of creation of the inner class object is not tied to the creation of the outer class object.
4.There is no potentially confusing “is-a” relationship with the inner class; it’s a separate entity.
------解决方案--------------------
作为外部类的一种组件支持外部类,比如java.util.Map和java.util.Map.Entry的关系
也可以定义一个函数对象来开展一个任务,比如java.util.TimerTask经常做一个匿名内部类。
也可以模拟多重继承
可以隐藏实现细节

其他还有些,用得少
------解决方案--------------------
你看看SWING这部分内容就明白了.
------解决方案--------------------
我也觉得这部分很难理解,慢慢体会下ls几位说的
------解决方案--------------------
thinking in java 3rd的第8章,内部类于控制框架那小节有个简单的使用内部类的例子,你可以先看看那个
------解决方案--------------------
。。。看看JTable会明白一些吧