boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Re: java inner class - 初学者问 (转载)
相关主题
请教一个Java编程问题
Refactoring long class step by step (2)
java inner class大家用的多么?我个人来说 很少用
Refactoring long class step by step (1)
C++ IDE under Linux
你身边有这样的人吗?--- code stylist
写Java程序不用IDE,那心灵得多强大啊
FP的教材是怎么误导人的
对PyCharm屈服了……
C++问题几个
相关话题的讨论汇总
话题: class话题: inner话题: 匿名话题: 复用话题: 初学者
进入Programming版参与讨论
1 (共1页)
z****e
发帖数: 54598
1
【 以下文字转载自 Java 讨论区 】
发信人: zhaoce (米高蜥蜴), 信区: Java
标 题: Re: java inner class - 初学者问
发信站: BBS 未名空间站 (Tue Feb 19 03:10:22 2013, 美东)
内部类和匿名类不是一回事
顺便,匿名类如果做出来了,你基本上想复用这部分代码就impossible了
这就是为什么大多数匿名类都出现在swing这种地方的原因
匿名类从本质上说就违反了oo的最基本的考虑,软件代码的复用
所以我也从来不屑什么匿名类匿名方法的使用
这也是为什么古德霸说这个是糖水的原因
换句话说,这个东西更多的是让写的人爽,但是别人看起来不太爽
g*****g
发帖数: 34805
2
This was not what I meant. Inner class is just inner class. Anonymous inner
class is just one of the kind. Basically there are 3 use cases of inner
class.
1. The class is simple and almost always created with the outer class, you
want a simple way to group these two classes. You can use a static public
inner class.
2. The class is never used outside of the outer class but you want to create
it in more than one places. private inner class is appropriate.
3. Same as 2 but you only need it in one place, anonymous inner class is
appropriate.
Personally I don't code Swing these days. But I use it a lot in multi-
threading program. e.g.
new Runnable() {
public void run() {
//some logic.
}
}

【在 z****e 的大作中提到】
: 【 以下文字转载自 Java 讨论区 】
: 发信人: zhaoce (米高蜥蜴), 信区: Java
: 标 题: Re: java inner class - 初学者问
: 发信站: BBS 未名空间站 (Tue Feb 19 03:10:22 2013, 美东)
: 内部类和匿名类不是一回事
: 顺便,匿名类如果做出来了,你基本上想复用这部分代码就impossible了
: 这就是为什么大多数匿名类都出现在swing这种地方的原因
: 匿名类从本质上说就违反了oo的最基本的考虑,软件代码的复用
: 所以我也从来不屑什么匿名类匿名方法的使用
: 这也是为什么古德霸说这个是糖水的原因

z****e
发帖数: 54598
3
那你这个匿名类怎么复用?
复用不能的代码其实用java写是有些不太爽的

inner
create

【在 g*****g 的大作中提到】
: This was not what I meant. Inner class is just inner class. Anonymous inner
: class is just one of the kind. Basically there are 3 use cases of inner
: class.
: 1. The class is simple and almost always created with the outer class, you
: want a simple way to group these two classes. You can use a static public
: inner class.
: 2. The class is never used outside of the outer class but you want to create
: it in more than one places. private inner class is appropriate.
: 3. Same as 2 but you only need it in one place, anonymous inner class is
: appropriate.

g*****g
发帖数: 34805
4
这个你是错的,这个匿名类本身是不能复用。但你别忘了用匿名内部类的前提就是这俩
类的逻辑都绑一块了,如果说A是outer class, B是inner class。这就相当于说B是A的
一部分,你不需要拿B去给C用。AB绑在一块是可以复用的。或者说B就像A一个扩展的
private method。你不能说private method不能复用所以不该用。
匿名内部类注重的是scope,因为B不能单独使用。做成内部类可以简化public API。如
果你发现需要B给C用,再独立出来也是可以的。软件工程提倡的是refactoring。不可
能所有的设计都一次合理。情况变化了改动是正常的。追求的是改只改一个地方。

【在 z****e 的大作中提到】
: 那你这个匿名类怎么复用?
: 复用不能的代码其实用java写是有些不太爽的
:
: inner
: create

1 (共1页)
进入Programming版参与讨论
相关主题
C++问题几个
一个multithreading 问题
如何优化这段C代码
多线程优化求助! (转载)
编程题又一道
问题一枚
FMP 进驻 Programming 版
java update main UI from child thread issue (转载)
c++的高级的东东在实际项目开发中到底有没有很大的应用阿?
Java 多线程 的架构如何改进?
相关话题的讨论汇总
话题: class话题: inner话题: 匿名话题: 复用话题: 初学者