- 浏览: 235820 次
- 性别:
- 来自: 上海
最新评论
-
iwindyforest:
pinocchio2mx 写道iwindyforest 写道H ...
VMware Workstation 11 或者 VMware Player 7安装MAC OS X 10.10 Yosemite -
nng119:
找不到设备的安装信息 这个问题怎么解决的?
VMware Workstation 11 或者 VMware Player 7安装MAC OS X 10.10 Yosemite -
pinocchio2mx:
iwindyforest 写道Hi pinocchio2mx ...
VMware Workstation 11 或者 VMware Player 7安装MAC OS X 10.10 Yosemite -
iwindyforest:
Hi pinocchio2mx 兄弟, 这个镜像是好的, 我安 ...
VMware Workstation 11 或者 VMware Player 7安装MAC OS X 10.10 Yosemite -
pinocchio2mx:
蛋疼啊,折腾一晚上还没搞定!网上的教程没一篇靠谱的,摸摸索索到 ...
VMware Workstation 11 或者 VMware Player 7安装MAC OS X 10.10 Yosemite
Inner Classes
创建
If an inner class has constructors, the compiler modifies them, adding a parameter for the outer class reference.
如果一个内部类没有构造函数, 编译器会改变它们, 加上一个外部类的引用作为参数.
Conversely, you can write the inner object constructor more explicitly, using the syntax
outerObject.new InnerClass(construction parameters)
For example,
ActionListener listener = this.new TimePrinter();
相反的, 你可以用更加显式的方法写内部类的构造函数, 语法是:
outerObject.new InnerClass(construction parameters)
例如:
ActionListener listener = this.new TimePrinter();
外部类的访问
In the preceding section, we explained the outer class reference of an inner class by calling it outer. Actually, the proper syntax for the outer reference is a bit more complex. The expression
OuterClass.this
外部类的访问语法为: OuterClass.this
内部类的访问
Note that you refer to an inner class as:
OuterClass.InnerClass
注意引用内部类的方法是: OuterClass.InnerClass
Local Inner Classes
局部内部类
定义在函数内部的内部类为局部内部类
public void start()
{
class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Date now = new Date();
System.out.println("At the tone, the time is " + now);
if (beep) Toolkit.getDefaultToolkit().beep();
}
}
ActionListener listener = new TimePrinter();
Timer t = new Timer(1000, listener);
t.start();
}
Local classes are never declared with an access specifier (that is, public or private). Their scope is always restricted to the block in which they are declared.
局部类不能以任一访问修饰符修饰. 它们的范围被限定在定义它的语句块内部.
Local classes have a great advantage: they are completely hidden from the outside world—not even other code in the TalkingClock class can access them. No method except start has any knowledge of the TimePrinter class.
局部类有一个非常大的优点: 他们对于外部世界完全隐藏, 甚至TalkingClock类内部的其余代码也不能访问他们. 除start()函数外的其余方法都不能获悉TimerPrinter局部类的访问.
Anonymous Inner Classes
匿名内部类
Any parameters used to construct the object are given inside the parentheses ( ) following the supertype name. In general, the syntax is
new SuperType(construction parameters)
{
inner class methods and data
}
Here, SuperType can be an interface, such as ActionListener; then, the inner class implements that interface. Or SuperType can be a class; then, the inner class extends that class.
父类构造函数的内部可以使用任意的参数. 一般来说, 语法为:
new SuperType(construction parameters)
{
inner class methods and data
}
这里, SuperType可以为接口, 比如ActionListener; 那么, 内部类将会实现此接口. 或者为一个类; 那么这个内部类将继承此类.
An anonymous inner class cannot have constructors because the name of a constructor must be the same as the name of a class, and the class has no name. Instead, the construction parameters are given to the superclass constructor. In particular, whenever an inner class implements an interface, it cannot have any construction parameters. Nevertheless, you must supply a set of parentheses as in
一个匿名内部类不能有多个构造函数, 因为构造函数必须同名函数, 但是匿名内部类是没有类名的. 取而代之的是, 匿名内部类的构造函数参数将会给予超类的构造函数. 特别的是, 无论什么时候一个匿名内部类实现接口都不能有任何带参数的构造函数. 然而, 你必须在其内部提供一系列数据.
Static Inner Classes
静态内部类
Occasionally, you want to use an inner class simply to hide one class inside another, but you don't need the inner class to have a reference to the outer class object. You can suppress the generation of that reference by declaring the inner class static.
有时候, 你想用一个内部类只是为了在另一个类中隐藏它, 但是你不需要这个内部类 访问外部类的索引. 你可以声明此内部类为静态的来一只外部类索引的生成.
Here is a typical example of where you would want to do this. Consider the task of computing the minimum and maximum value in an array. Of course, you write one method to compute the minimum and another method to compute the maximum. When you call both methods, then the array is traversed twice. It would be more efficient to traverse the array only once, computing both the minimum and the maximum simultaneously.
这里有一个典型的例子. 设想计算一个数组的最大最小值. 当然, 你写了一个方法去计算最小值, 另一计算最大值. 当你调用这两个方法是, 这个数字会遍历两次. 只遍历数字一次就同时取得数组的最大最小值的算法是更有效率的.
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (double v : values)
{
if (min > v) min = v;
if (max < v) max = v;
}
However, the method must return two numbers. We can achieve that by defining a class Pair that holds two values:
尽管如此, 这个方法必须返回两个值. 我们可以定义一个Pair类来实现保存这两个值.
class Pair
{
public Pair(double f, double s)
{
first = f;
second = s;
}
public double getFirst() { return first; }
public double getSecond() { return second; }
private double first;
private double second;
}
The minmax function can then return an object of type Pair.
那么minmax()函数就能够返回一个Pair类型的对象了.
class ArrayAlg
{
public static Pair minmax(double[] values)
{
. . .
return new Pair(min, max);
}
}
The caller of the function uses the getFirst and getSecond methods to retrieve the answers:
这个函数的调用者使用Pair类的getFirst()和getSecond()函数来返回结果.
Pair p = ArrayAlg.minmax(d);
System.out.println("min = " + p.getFirst());
System.out.println("max = " + p.getSecond());
Of course, the name Pair is an exceedingly common name, and in a large project, it is quite possible that some other programmer had the same bright idea, except that the other programmer made a Pair class that contains a pair of strings. We can solve this potential name clash by making Pair a public inner class inside ArrayAlg. Then the class will be known to the public as ArrayAlg.Pair:
当然, Pair类名是一个非常常见的名字, 在一个大项目中, 很可能其他程序员有着相同的注意. 我们可以通过吧Pair声明为public来解决这个潜在的问题. 那么这个类将会以: ArrayAlg.Pair来访问.
ArrayAlg.Pair p = ArrayAlg.minmax(d);
However, unlike the inner classes that we used in previous examples, we do not want to have a reference to any other object inside a Pair object. That reference can be suppressed by declaring the inner class static:
尽管如此, 不像我们以前例子中用到的内部类, 我们不想再Pair类中拥有任何其它类的引用, 因此将这个内部类声明为static可以抑制索引的生成.
class ArrayAlg
{
public static class Pair
{
. . .
}
. . .
}
Of course, only inner classes can be declared static. A static inner class is exactly like any other inner class, except that an object of a static inner class does not have a reference to the outer class object that generated it. In our example, we must use a static inner class because the inner class object is constructed inside a static method:
当然, 只有静态内部类可以被声明为static. 一个静态内部类出来不含其它类的引用之外同其他类型的内部类没有区别.在我们的例子中, 我们必须使用静态内部类, 因为它被用在了静态方法中.
public static Pair minmax(double[] d)
{
. . .
return new Pair(min, max);
}
Had the Pair class not been declared as static, the compiler would have complained that there was no implicit object of type ArrayAlg available to initialize the inner class object.
如果Pair类没有被声明为静态的, 那么编译器便会报告没有显示可以的静态内部类初始化对象.
NOTE
You use a static inner class whenever the inner class does not need to access an outer class object. Some programmers use the term nested class to describe static inner classes.
当一个内部类不需要访问任何外部对象时你需要声明此类为静态内部类. 有些程序员使用终端嵌套类去描述静态内部类.
Inner classes that are declared inside an interface are automatically static and public.
定义在一个接口中的静态类会被自动声明为static和public.
创建
If an inner class has constructors, the compiler modifies them, adding a parameter for the outer class reference.
如果一个内部类没有构造函数, 编译器会改变它们, 加上一个外部类的引用作为参数.
Conversely, you can write the inner object constructor more explicitly, using the syntax
outerObject.new InnerClass(construction parameters)
For example,
ActionListener listener = this.new TimePrinter();
相反的, 你可以用更加显式的方法写内部类的构造函数, 语法是:
outerObject.new InnerClass(construction parameters)
例如:
ActionListener listener = this.new TimePrinter();
外部类的访问
In the preceding section, we explained the outer class reference of an inner class by calling it outer. Actually, the proper syntax for the outer reference is a bit more complex. The expression
OuterClass.this
外部类的访问语法为: OuterClass.this
内部类的访问
Note that you refer to an inner class as:
OuterClass.InnerClass
注意引用内部类的方法是: OuterClass.InnerClass
Local Inner Classes
局部内部类
定义在函数内部的内部类为局部内部类
public void start()
{
class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Date now = new Date();
System.out.println("At the tone, the time is " + now);
if (beep) Toolkit.getDefaultToolkit().beep();
}
}
ActionListener listener = new TimePrinter();
Timer t = new Timer(1000, listener);
t.start();
}
Local classes are never declared with an access specifier (that is, public or private). Their scope is always restricted to the block in which they are declared.
局部类不能以任一访问修饰符修饰. 它们的范围被限定在定义它的语句块内部.
Local classes have a great advantage: they are completely hidden from the outside world—not even other code in the TalkingClock class can access them. No method except start has any knowledge of the TimePrinter class.
局部类有一个非常大的优点: 他们对于外部世界完全隐藏, 甚至TalkingClock类内部的其余代码也不能访问他们. 除start()函数外的其余方法都不能获悉TimerPrinter局部类的访问.
Anonymous Inner Classes
匿名内部类
Any parameters used to construct the object are given inside the parentheses ( ) following the supertype name. In general, the syntax is
new SuperType(construction parameters)
{
inner class methods and data
}
Here, SuperType can be an interface, such as ActionListener; then, the inner class implements that interface. Or SuperType can be a class; then, the inner class extends that class.
父类构造函数的内部可以使用任意的参数. 一般来说, 语法为:
new SuperType(construction parameters)
{
inner class methods and data
}
这里, SuperType可以为接口, 比如ActionListener; 那么, 内部类将会实现此接口. 或者为一个类; 那么这个内部类将继承此类.
An anonymous inner class cannot have constructors because the name of a constructor must be the same as the name of a class, and the class has no name. Instead, the construction parameters are given to the superclass constructor. In particular, whenever an inner class implements an interface, it cannot have any construction parameters. Nevertheless, you must supply a set of parentheses as in
一个匿名内部类不能有多个构造函数, 因为构造函数必须同名函数, 但是匿名内部类是没有类名的. 取而代之的是, 匿名内部类的构造函数参数将会给予超类的构造函数. 特别的是, 无论什么时候一个匿名内部类实现接口都不能有任何带参数的构造函数. 然而, 你必须在其内部提供一系列数据.
Static Inner Classes
静态内部类
Occasionally, you want to use an inner class simply to hide one class inside another, but you don't need the inner class to have a reference to the outer class object. You can suppress the generation of that reference by declaring the inner class static.
有时候, 你想用一个内部类只是为了在另一个类中隐藏它, 但是你不需要这个内部类 访问外部类的索引. 你可以声明此内部类为静态的来一只外部类索引的生成.
Here is a typical example of where you would want to do this. Consider the task of computing the minimum and maximum value in an array. Of course, you write one method to compute the minimum and another method to compute the maximum. When you call both methods, then the array is traversed twice. It would be more efficient to traverse the array only once, computing both the minimum and the maximum simultaneously.
这里有一个典型的例子. 设想计算一个数组的最大最小值. 当然, 你写了一个方法去计算最小值, 另一计算最大值. 当你调用这两个方法是, 这个数字会遍历两次. 只遍历数字一次就同时取得数组的最大最小值的算法是更有效率的.
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (double v : values)
{
if (min > v) min = v;
if (max < v) max = v;
}
However, the method must return two numbers. We can achieve that by defining a class Pair that holds two values:
尽管如此, 这个方法必须返回两个值. 我们可以定义一个Pair类来实现保存这两个值.
class Pair
{
public Pair(double f, double s)
{
first = f;
second = s;
}
public double getFirst() { return first; }
public double getSecond() { return second; }
private double first;
private double second;
}
The minmax function can then return an object of type Pair.
那么minmax()函数就能够返回一个Pair类型的对象了.
class ArrayAlg
{
public static Pair minmax(double[] values)
{
. . .
return new Pair(min, max);
}
}
The caller of the function uses the getFirst and getSecond methods to retrieve the answers:
这个函数的调用者使用Pair类的getFirst()和getSecond()函数来返回结果.
Pair p = ArrayAlg.minmax(d);
System.out.println("min = " + p.getFirst());
System.out.println("max = " + p.getSecond());
Of course, the name Pair is an exceedingly common name, and in a large project, it is quite possible that some other programmer had the same bright idea, except that the other programmer made a Pair class that contains a pair of strings. We can solve this potential name clash by making Pair a public inner class inside ArrayAlg. Then the class will be known to the public as ArrayAlg.Pair:
当然, Pair类名是一个非常常见的名字, 在一个大项目中, 很可能其他程序员有着相同的注意. 我们可以通过吧Pair声明为public来解决这个潜在的问题. 那么这个类将会以: ArrayAlg.Pair来访问.
ArrayAlg.Pair p = ArrayAlg.minmax(d);
However, unlike the inner classes that we used in previous examples, we do not want to have a reference to any other object inside a Pair object. That reference can be suppressed by declaring the inner class static:
尽管如此, 不像我们以前例子中用到的内部类, 我们不想再Pair类中拥有任何其它类的引用, 因此将这个内部类声明为static可以抑制索引的生成.
class ArrayAlg
{
public static class Pair
{
. . .
}
. . .
}
Of course, only inner classes can be declared static. A static inner class is exactly like any other inner class, except that an object of a static inner class does not have a reference to the outer class object that generated it. In our example, we must use a static inner class because the inner class object is constructed inside a static method:
当然, 只有静态内部类可以被声明为static. 一个静态内部类出来不含其它类的引用之外同其他类型的内部类没有区别.在我们的例子中, 我们必须使用静态内部类, 因为它被用在了静态方法中.
public static Pair minmax(double[] d)
{
. . .
return new Pair(min, max);
}
Had the Pair class not been declared as static, the compiler would have complained that there was no implicit object of type ArrayAlg available to initialize the inner class object.
如果Pair类没有被声明为静态的, 那么编译器便会报告没有显示可以的静态内部类初始化对象.
NOTE
You use a static inner class whenever the inner class does not need to access an outer class object. Some programmers use the term nested class to describe static inner classes.
当一个内部类不需要访问任何外部对象时你需要声明此类为静态内部类. 有些程序员使用终端嵌套类去描述静态内部类.
Inner classes that are declared inside an interface are automatically static and public.
定义在一个接口中的静态类会被自动声明为static和public.
发表评论
-
利用归并排序算法对大文件进行排序
2015-01-25 20:59 5634归并排序算法介绍,请参照Wikipeida zh. ... -
try-catch影响性能吗?
2014-07-09 17:06 1686try-catch会影响性能吗? try-catch放在循 ... -
Java Concurrency In Practice Learning Note
2014-06-17 11:13 0Frameworks introduce concurre ... -
Java collections overview
2014-03-21 10:37 873Fromp: http://java-performance ... -
JVM Learning Note 4 -- HotSpot JVM Options List
2014-02-26 11:08 1036From: http://www.oracle.c ... -
Eclispe 性能优化
2014-02-25 15:07 945我目前Eclipse所在的机器环境是windows x32 ... -
JVM Learning Note 3 -- JVM Performance Monitor Tools
2014-02-25 12:25 1179JDK Provided Tools Comman ... -
JVM Performance Monitor Tools
2014-02-25 11:12 5JDK Provide Tools Command Li ... -
JVM Learning Note 2 -- Garbage Collection and Memory Allocation Strategy
2014-02-18 14:57 1767Method to check if an object ... -
JVM Learning Note 1 -- Run-Time Data Areas and Object Representation
2014-02-11 00:44 881Run-Time Data Areas Clas ... -
String.intern()方法
2014-02-10 16:22 789intern public String intern() ... -
GlobalKnowledge: 2013 IT 技能薪水报告
2014-01-09 18:24 1881from: GlobalKnowledge http:/ ... -
OLTP Vs OLAP
2014-01-02 13:42 841OLTP vs. OLAP We can divide ... -
java的字符串拼接
2013-12-31 16:43 817每当我用+运算符拼接字符串时, 总有人跟我提出你应该 ... -
45 Useful JavaScript Tips, Tricks and Best Practices
2013-12-31 11:08 10227By Saad Mousliki ... -
面试10大算法汇总+常见题目解答
2013-12-16 18:03 2005面试10大算法汇总+常见题目解答 最近更新 ... -
面试关于HashMap的工作原理
2013-12-16 17:58 1985先来些简单的问题 “你用过HashMap吗?” ... -
处理 InterruptedException
2013-11-01 16:28 750这样的情景您也许并不陌生:您在编写一个测试程序,程序需要暂 ... -
Java正确处理InterruptedException的方法
2013-11-01 16:24 805要想讨论正确处理Interr ... -
Overview To CMMI v1.3
2013-07-05 18:15 919What is CMMI? CMMI® (Capa ...
相关推荐
根据老师讲解写的笔记
### Java学习笔记——内部类详解 #### 一、引言 Java中的内部类是一个非常有用但又容易让人感到困惑的概念。内部类本质上是在另一个类的内部定义的类,它可以访问外部类的所有成员变量和方法,甚至是私有成员。...
redis学习笔记redis 是一个开源的 key-value 数据库。它又经常被认为是一个数据结构服务器。 因为它的 value 不仅包括基本的 string 类型还有 list,set ,sorted set 和 hash 类型。当 然这些类型的元素也都是 string...
属性的声明就像在类内部定义变量一样,而方法的声明则类似于函数,但属于类的范围。例如,我们可以添加一个获取年龄的方法: ```php Class Person { var $name; var $age; function getAge() { return $this->...
内部类可以分为四种类型:静态内部类、成员内部类(非静态内部类)、局部内部类和匿名内部类。 1. **静态内部类**: 静态内部类与普通的成员内部类不同,它不持有对外部类的引用。因此,可以像其他静态成员一样,...
本人参加linux培训时的内部学习笔记,绝对是不可多得的学习资料!
"Java学习笔记——良葛格"是一份专为初学者设计的教程资料,由良葛格精心编写,旨在帮助读者掌握JDK5.0版本的Java基础知识。JDK(Java Development Kit)是Java开发的核心工具集,包含了编译器、调试器和运行环境等...
【Java学习笔记Markdown版】是针对Java初学者和进阶者的一份详尽教程,以Markdown格式编写,便于阅读和整理。Markdown是一种轻量级的标记语言,它允许用户使用易读易写的纯文本格式编写文档,然后转换成结构化的HTML...
学习笔记通常包括了基础概念、关键特性、实用技巧以及常见问题的解决方法。 【标签】"h5"、"前端"、"学习笔记"进一步明确了内容的重点。"h5"即HTML5,是前端开发的核心;"前端"意味着这些笔记涉及的是用户可见和...
这份“非常详细JavaSE学习笔记.rar”压缩包显然是一份全面的Java SE学习资源,包含了从基础知识到高级特性的全方位讲解。下面,我们将详细探讨这份笔记可能涵盖的关键知识点。 1. **Java起源与环境搭建**:笔记可能...
本知识点的标题为“Java学习笔记(必看经典)”,意味着所整理的内容是针对Java初学者的一系列核心概念和原理的总结。 首先,面向对象编程是Java语言的核心,它与传统的面向过程编程有显著的不同。面向对象编程强调的...
Go语言,又称Golang,是一种静态类型的编程语言,由Google开发,于2007年首次对外公布,并在2009年进行了...通过本学习笔记的内容,我们可以对Go语言有一个全面而系统的认识,为深入学习和应用Go语言打下坚实的基础。
JAVA学习笔记.pdf 中讲解了JAVA语言的基础知识,包括类的基本知识、成员变量、成员方法、类的实例、内部类、匿名类、接口、包等。 类的基本知识 在JAVA中,类是对象的蓝图,类的声明语法为:[访问控制符] class ...
Redis学习笔记 Redis是基于键值对存储的NoSQL数据库,可以用来存储和检索数据。下面是Redis的基础知识点: 基础命令 * set key value:保存一个数据,重复set相同的key只会保存最新的value * get key:获取一个...
Hibernate学习笔记整理 以下是 Hibernate 框架的详细知识点: Hibernate 介绍 Hibernate 是一个 ORM(Object-Relational Mapping)框架,用于将 Java 对象映射到数据库表中。它提供了一个简洁的方式来访问和操作...
这个“redis学习笔记.zip”压缩包很可能是包含了关于Redis的学习资料,可能包括概念解释、操作教程、实践案例等内容,适合初学者和有一定基础的学习者参考。 Redis的学习可以分为以下几个主要部分: 1. **基础知识...
根据提供的信息,我们可以总结出这份文档是关于Go语言学习笔记的部分内容,主要涵盖了Go语言的基础概念、语法结构、数据类型以及并发模型等关键知识点。以下是对这些知识点的详细解析: ### Go语言概述 Go(也称作...
### C++ 学习笔记精华版 #### 一、C++ 语言概述 **1、历史背景** - **C++ 的江湖地位** - Java、C、C++、Python、C# 是当前主流的编程语言之一,而 C++ 在这些语言中以其高效性和灵活性著称。 - **C++ 之父 ...
### JavaSE内部学习笔记知识点概览 #### 一、Java语言概述 - **1.1 基础常识** - **软件分类**:软件分为系统软件和应用软件两大类。系统软件支持计算机硬件正常工作,包括操作系统、设备驱动程序等;应用软件则...