One common use of a static member class is as a public helper class, useful
only in conjunction with its outer class. For example, consider an enum describing
the operations supported by a calculator (Item 30). The Operation enum should
be a public static member class of the Calculator class. Clients of Calculator
could then refer to operations using names like Calculator.Operation.PLUS and
Calculator.Operation.MINUS.
One common use of a nonstatic member class is to define an Adapter
[Gamma95, p. 139] that allows an instance of the outer class to be viewed as an
instance of some unrelated class. For example, implementations of the Map interface
typically use nonstatic member classes to implement their collection views,
which are returned by Map’s keySet, entrySet, and values methods. Similarly,
implementations of the collection interfaces, such as Set and List, typically use
nonstatic member classes to implement their iterators:
// Typical use of a nonstatic member class
public class MySet<E> extends AbstractSet<E> {
... // Bulk of the class omitted
public Iterator<E> iterator() {
return new MyIterator();
}
private class MyIterator implements Iterator<E> {
...
}
}
If you declare a member class that does not require access to an enclosing
instance, always put the static modifier in its declaration, making it a static
rather than a nonstatic member class. If you omit this modifier, each instance will
have an extraneous reference to its enclosing instance. Storing this reference costs
time and space, and can result in the enclosing instance being retained when it
would otherwise be eligible for garbage collection (Item 6). And should you ever
need to allocate an instance without an enclosing instance, you’ll be unable to do
so, as nonstatic member class instances are required to have an enclosing instance.
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.
Anonymous classes are unlike anything else in the Java programming language.
As you would expect, an anonymous class has no name. It is not a member
of its enclosing class. Rather than being declared along with other members, it is
simultaneously declared and instantiated at the point of use. Anonymous classes
are permitted at any point in the code where an expression is legal. Anonymous
classes have enclosing instances if and only if they occur in a nonstatic context.
But even if they occur in a static context, they cannot have any static members.
There are many limitations on the applicability of anonymous classes. You
can’t instantiate them except at the point they’re declared. You can’t perform
instanceof tests or do anything else that requires you to name the class. You
can’t declare an anonymous class to implement multiple interfaces, or to extend a
class and implement an interface at the same time. Clients of an anonymous class
can’t invoke any members except those it inherits from its supertype. Because
anonymous classes occur in the midst of expressions, they must be kept short—
about ten lines or fewer—or readability will suffer.
One common use of anonymous classes is to create function objects (Item 21)
on the fly. For example, the sort method invocation on page 104 sorts an array of
strings according to their length using an anonymous Comparator instance.
Another common use of anonymous classes is to create process objects, such as
Runnable, Thread, or TimerTask instances. A third common use is within static
factory methods (see the intArrayAsList method in Item 18).
Local classes are the least frequently used of the four kinds of nested classes. A
local class can be declared anywhere a local variable can be declared and obeys the
same scoping rules. Local classes have attributes in common with each of the other
kinds of nested classes. Like member classes, they have names and can be used
repeatedly. Like anonymous classes, they have enclosing instances only if they are
defined in a nonstatic context, and they cannot contain static members. And like
anonymous classes, they should be kept short so as not to harm readability.
To recap, there are four different kinds of nested classes, and each has its
place. If a nested class needs to be visible outside of a single method or is too long
to fit comfortably inside a method, use a member class. If each instance of the
member class needs a reference to its enclosing instance, make it nonstatic; otherwise,
make it static. Assuming the class belongs inside a method, if you need to
create instances from only one location and there is a preexisting type that characterizes
the class, make it an anonymous class; otherwise, make it a local class.
分享到:
相关推荐
Item 24: Favor static member classes over nonstatic Item 25: Limit source files to a single top-level class 5 Generics Item 26: Don’t use raw types Item 27: Eliminate unchecked warnings Item 28: ...
AdapterDelegates, 用于RecyclerView适配器的"Favor composition over inheritance" AdapterDelegates阅读这个项目的动机在我的博客文章。依赖项这里库在 Maven 中心可用:compile '...
and those object references are used in favor of using string handle (such as GHUser.isMemberOf(GHOrganization) instead of GHUser.isMemberOf(String)) The library supports both github....
Provide a data store with a graph abstraction (vertexes and edges), not keys/values Explicitly favor efficiency and availability over consistency
46 PROC. OF THE 12th PYTHON IN SCIENCE CONF. (SCIPY 2013)Pythran: Enabling Static Optimization of Scientific Python ProgramsSerge Guelton∗†, Pierrick Brunet‡, Alan Raynaud‡, ...them in favor of powe
Scoping Namespaces Nested Classes Nonmember, Static Member, and Global Functions Local Variables Static and Global Variables Classes Doing Work in Constructors Default Constructors Explicit ...
(WROX官方网站提供)What is this book about? ASP.NET 2 Beta Preview is timed ... Examples throughout the book do not favor one developer over another. Instead, every example is provided in both languages.
【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the(72页),资源名称:【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the...
POLAR品牌说明书
java snmp 源码 ~ C/C++ CppCon2017会议资料 A curated list of awesome C/C++ performance optimization resources. c++ lib,大杂烩,什么都有 c++ http lib c++ lib 嵌入的高速数据同步库 ...内存型HAT-trie
… has become a hot topic among people ...As far as I am concerned, my favor goes to the first/second view. Admittedly, …but it doesn’t follow that… Only if … can we…, just as the saying goes…
> Favor composition over inheritance. LovelyDialog doesn't subclass any Dialog related classes, it is just a lightweight extensible wrapper for Dialog and manipulations with custom view. If you would ...
表演者 + 注意力机制的简单 Numpy 实现在Rethinking Attention with Performers ,以及在我的文章中复制实验的代码: 。
Favor A easy way of using Android SharedPreferences. How to use this library Using Gradle compile 'com.cocosw:favor:0.2.0@aar' Using Maven com.cocosw favor 0.2.0 apklib API 1 Define a ...
domeafavor-android 爱彼迎 - Android
NULL 博文链接:https://z466459262.iteye.com/blog/901207
In general, Ruby programmers tend to favor the the command line for running their code, and they tend to use text editors rather than IDEs. * Ruby runs on Windows, Linux, and Macs.
Many individual instructors — and, in some cases, entire universities — are gravitating towards the use of comprehensive ...report does not favor one LMS over another as the foundation for a CALMS.
El Favor de las GuerrerasDespliegue del entorno安装Anaconda和Librerias 安装在Descargar 在安装Ananada时,应注意以下几点: - Python 3.7.9- Spyder 3.3.6- Tensorflow 2.3.0Desde Anaconda Prompt,赞比亚广播...
Qt 类设计原则,内容不多,待续 – 面向接口编程而不是实现 (Code to an interface rather than to an implementation.) – 优先使用组合而非继承 (Favor Composition Over Inheritance)