- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想知道封闭类可以创建多少个静态成员类实例。我假设只有一个,但 Bloch 的以下摘录对我来说没有意义。
引用 Joshua Bloch 的 Effective Java - Item 22*:优先使用静态成员类而不是非静态成员类。
A common use of private static member classes is to represent components of the object represented by their enclosing class. For example, consider a Map instance, which associates keys with values. Many Map implementations have an internal Entry object for each key-value pair in the map. While each entry is associated with a map, the methods on an entry (getKey, getValue and setValue) do not need access to the map. Therefore, it would be wasteful to use a nonstatic member class to represent entries: a private static member class is best. If you accidentally omit the static modifier in the entry declaration, the map will still work, but each entry will contain a superfluous reference to the map, which wastes space and time.
他说map为map中的每个键值对创建一个Entry对象,即静态成员类的多个实例。
所以我的假设是错误的!也就是说我对静态成员类的理解是错误的。每个人都知道静态成员变量的行为方式,例如经典的静态最终字符串——对象只有一个实例。
这是否意味着在实例化封闭对象时实际上并未实例化静态成员类?
那么在那种情况下,Map 使用静态成员类作为 Entry 有什么意义呢?为什么不直接使用 API 上的接口(interface)?然后所有其他 Collections 类都可以提供它自己的实现。
[*] 才发现是我手头PDF版书的第18条
最佳答案
这是对 static
关键字的常见误解。
当您将 static
与变量一起使用时,这意味着此类的所有对象都只有其中一个 或类似的东西。
static Object thereWillBeOnlyOne = new Object();
然而,在内部类的上下文中,它意味着完全不同的东西。 static
内部类与封闭类的对象没有联系,而非静态内部类则有。
static
内部类:
public class TrieMap<K extends CharSequence, V> extends AbstractMap<K, V> implements Map<K, V> {
private static class Entry<K extends CharSequence, V> implements Map.Entry<K, V> {
我的 TrieMap
类使用的 Map.Entry
类不需要引用创建它的对象,因此可以将其设为静态
保存不必要的引用。
非静态
内部类:
public final class StringWalker implements Iterable<Character> {
// The iteree
private final String s;
// Where to get the first character from.
private final int start;
// What to add to i (usually +/- 1).
private final int step;
// What should i be when we stop.
private final int stop;
// The Character iterator.
private final class CharacterIterator implements Iterator<Character> {
// Where I am.
private int i;
// The next character.
private Character next = null;
CharacterIterator() {
// Start at the start.
i = start;
}
public boolean hasNext() {
if (next == null) {
if (step > 0 ? i < stop : i > stop) {
next = s.charAt(i);
i += step;
}
}
return next != null;
}
StringWalker
对象中的CharacterIterator
指的是要迭代的字符串作为s
,它在StringWalker< 中只存在一次
对象。因此,我可以创建一个 StringWalker
的许多迭代器,它们都遍历相同的字符串。
这种看似不合逻辑的双重性源于 C
中 static
关键字的使用。
在 C
中你可以(或者至少曾经能够)做:
void doSomething () {
static int x = 1;
if ( x < 3 ) {
} else {
}
x += 1;
}
并且每次您调用该函数时,x
都会保持上次离开时的状态 - 在这种情况下会递增。
其概念是,static
关键字指示变量被其封闭 block 范围内封闭,但被其父 block 语义上封闭。 IE。上面的代码大致相当于:
int x = 1;
void doSomething () {
if ( x < 3 ) {
} else {
}
x += 1;
}
但是 x
只允许在函数内部被引用。
将这个概念推进到 Java
中,现在事情变得更有意义了。 static
内部类的行为与它在类外部声明的完全一样,而非 static
内部类与其封闭实例的绑定(bind)更加紧密 - 实际上它可以引用该实例直接。
还有:
class Thing {
static Object thereWillBeOnlyOne = new Object();
行为很像
Object thereWillBeOnlyOne = new Object();
class Thing {
如果它是合法的。
本课到此结束。
关于java - Bloch Effective Java - 支持静态类而不是非静态类 - 有多少个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24953370/
我的 processmaker 安装遇到了一些问题。我正在尝试使用本指南 [url]http://wiki.processmaker.com/index.php/ProcessMaker_Ubuntu
我正在使用 ShareKit。发送 SMS 消息使用 MFMessageComposeViewController,用户看到标题“文本”。我想将该标题更改为更能反射(reflect)实际可用内容的内容
我需要在我的一个针对 Gingerbread 的 Android 应用程序中使用操作栏和 fragment 的组合。所以我使用了 v7 支持库中的操作栏和 v4 支持库中的 fragment ,并使用
我明白为什么浏览器 vendor 不想帮助我阻止他们的 UI 线程。但是,我不明白为什么会有: Web Workers 中没有 sleep (2) 没有同步 WebSockets API 有一个syn
最近我的组织正在考虑使用 Docker。我们组使用的是cloudera CDH 5.1.2。 1) cloudera 是否与 Docker 容器兼容?2) docker 和cloudera 组合是否存
我正在尝试通过编译在 Mac 上安装 rsync 3.2.3。但是,我想安装所有功能。为此,它需要一些库,此处 ( https://download.samba.org/pub/rsync/INSTA
我一直在使用 PyDev 成功运行 nose 测试,并想试试 nose2。 所以我安装了它 pip install nose2 复制/粘贴来自 http://nose2.info/ 的示例代码进入名为
我想知道 LLVM 中是否有任何函数/方法可以在 LLVM IR 中添加 Open-MP 构造。 llvm-3.0 是否仍然支持 OpenMP 指令? 最佳答案 OpenMP 是一种高级语言扩展。因此
我对 CUDA 编程非常陌生。我正在浏览 SDK 附带的示例。我能够编译代码,但是当我运行它时,出现以下错误: "clock.cu(177) : CUDA Runtime API error 38:
RStudio 是用于 R 开发的出色 IDE。我想知道是否有任何方法可以很好地支持 HiDPI 分辨率? 我目前有 13 英寸显示器和 3200x1800 分辨率,甚至很难阅读 RStudio 选项
我正在寻找一种有助于为 Django 项目提供 RDF 支持的工具。 到目前为止,我发现了两个: django-rdf - 最后一次修改是在 4 年前,所以它看起来像是一个死项目。 djubby -
我刚刚尝试了一些 JS 核心原则,发现引擎评估链接的关系运算符而不会引发错误。相反,他们以我自己无法理解的方式进行评估。 console.log(1 4 > 3 > 2 > 1); //false,
我知道 etexteditor 和 vim/emacs。 是否有任何其他 Windows 编辑器支持类似 textmate 的片段(例如,您编写触发词,按 Tab,它更改为某些内容,再次按 Tab,它
我正在尝试找出验证给定集群的网络策略配置的最佳方法。 According to the documentation Network policies are implemented by the ne
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
Z3 会支持 AUFBV 吗? 对于以下脚本: (set-logic AUFBV) (declare-fun x () (_ BitVec 16)) (declare-const t (Array (
使用分部类编写 NUnit 测试的优缺点是什么? 我要开始了: 亲:可以测试私有(private)方法 缺点:TDD 不再可能了 还有什么? 最佳答案 缺点:要么您必须测试与您发布的版本不同的构建,要
它很容易(对于 90% 的 aop 特性)在没有任何语言本身支持的情况下做到这一点,就像大多数动态语言如 python 和 ruby 一样。然而,Dojo在 1.3.2 上直接支持它.最新版本发生
我在我的 android 应用程序中使用亚洲字符,我已经了解到某些字符无法显示,因为系统字体不支持它们。我查询了一个包含亚洲字符的数据库,并且经常检索到无法显示的标志。这些情况对我的应用程序来说通常不
你好,我想实现一个控件,我想在用户键入@字符时启用该控件,直到未填充运行文本中的空格为止,它应该显示用户列表,@符号后键入的文本应该显示基于键盘字符的建议,就像我们在上面看到的那样Twitter 或
我是一名优秀的程序员,十分优秀!