嵌套类,摘自:
http://www.ntu.edu.sg/home/ehchua/programmin/java/J4a_GUI.html

A nested class has these properties:

  1. A nested class is a proper class. That is, it could contain constructors, member variables and member methods. You can create an instance of a nested class via the new operator and constructor.
  2. A nested class is a member of the outer class, just like any member variables and methods defined inside a class.
  3. Most importantly, a nested class can access the private members (variables/methods) of the enclosing outer class, as it is at the same level as these private members. This is the property that makes inner class useful.
  4. A nested class can have private, public, protected, or the default access, just like any member variables and methods defined inside a class. A private inner class is only accessible by the enclosing outer class, and is not accessible by any other classes. [An top-level outer class cannot be declared private, as no one can use a private outer class.]
  5. A nested class can also be declared static, final or abstract, just like any ordinary class.
  6. A nested class is NOT a subclass of the outer class. That is, the nested class does not inherit the variables and methods of the outer class. It is an ordinary self-contained class. [Nonetheless, you could declare it as a subclass of the outer class, via keyword "extends OuterClassName", in the nested class's definition.]

The usages of nested class are:

  1. To control visibilities (of the member variables and methods) between inner/outer class. The nested class, being defined inside an outer class, can access private members of the outer class.
  2. To place a piece of class definition codes closer to where it is going to be used, to make the program clearer and easier to understand.
  3. For namespace management.
1.
import java.awt.*;
import java.awt.event.*; // An AWT GUI program inherits from the top-level container java.awt.Frame
public class AWTCounterNamedInnerClass extends Frame {
// This class is NOT a ActionListener, hence, it does not implement ActionListener // The event-handler actionPerformed() needs to access these "private" variables
private TextField tfCount;
private int count = 0; /** Constructor to setup the GUI */
public AWTCounterNamedInnerClass () {
setLayout(new FlowLayout()); // "super" Frame sets to FlowLayout
add(new Label("Counter")); // anonymous instance of Label
tfCount = new TextField("0", 10);
tfCount.setEditable(false); // read-only
add(tfCount); // "super" Frame adds tfCount Button btnCount = new Button("Count");
add(btnCount); // "super" Frame adds btnCount // Construct an anonymous instance of BtnCountListener (a named inner class).
// btnCount adds this instance as a ActionListener.
btnCount.addActionListener(new BtnCountListener()); setTitle("AWT Counter");
setSize(250, 100);
setVisible(true);
} /** The entry main method */
public static void main(String[] args) {
new AWTCounterNamedInnerClass(); // Let the constructor do the job
} /**
* BtnCountListener is a "named inner class" used as ActionListener.
* This inner class can access private variables of the outer class.
*/
private class BtnCountListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
++count;
tfCount.setText(count + "");
}
}
} 2. import java.awt.*;
import java.awt.event.*; // An AWT GUI program inherits from the top-level container java.awt.Frame
public class AWTCounterAnonymousInnerClass extends Frame {
// This class is NOT a ActionListener, hence, it does not implement ActionListener // The event-handler actionPerformed() needs to access these private variables
private TextField tfCount;
private int count = 0; /** Constructor to setup the GUI */
public AWTCounterAnonymousInnerClass () {
setLayout(new FlowLayout()); // "super" Frame sets to FlowLayout
add(new Label("Counter")); // an anonymous instance of Label
tfCount = new TextField("0", 10);
tfCount.setEditable(false); // read-only
add(tfCount); // "super" Frame adds tfCount Button btnCount = new Button("Count");
add(btnCount); // "super" Frame adds btnCount // Construct an anonymous instance of an anonymous class.
// btnCount adds this instance as a ActionListener.
btnCount.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
++count;
tfCount.setText(count + "");
}
}); setTitle("AWT Counter");
setSize(250, 100);
setVisible(true);
} /** The entry main method */
public static void main(String[] args) {
new AWTCounterAnonymousInnerClass(); // Let the constructor do the job
}
}

nest(inner) class的更多相关文章

  1. JSONModel 嵌套字典数组 JSONModel nest NSDictionary NSArray

    JSONModel 嵌套字典数组  JSONModel nest NSDictionary NSArray

  2. Nest查询示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. NEST.net Client For Elasticsearch简单应用

    NEST.net Client For Elasticsearch简单应用 由于最近的一个项目中的搜索部分要用到 Elasticsearch 来实现搜索功能,苦于英文差及该方面的系统性资料不好找,在实 ...

  4. Elasticsearch .Net Client NEST使用说明 2.x

    Elasticsearch .net client NEST使用说明 2.x Elasticsearch.Net与NEST是Elasticsearch为C#提供的一套客户端驱动,方便C#调用Elast ...

  5. Elasticsearch .Net Client NEST 多条件查询示例

    Elasticsearch .Net Client NEST 多条件查询示例 /// <summary> /// 多条件搜索例子 /// </summary> public c ...

  6. Elasticsearch .Net Client NEST 索引DataSet数据

    NEST 索引DataSet数据,先序列化然后转成dynamic 类型进行索引: /// <summary> /// 索引dataset /// </summary> /// ...

  7. nest 'for' loop.

    /* nest for loop demo. Note that,'upside' triangle controls 'inner condition'. */ import kju.print.P ...

  8. Nest客户端的基本使用方法

    通过Nuget安装好Nest的相关Dll,之后我们就可以开始了, 1.初始化Nest客户端 string indexName = "customer"; Uri uri = new ...

  9. NEST 中的时间单位

    Time units 英文原文地址:Time units 与 Elasticsearch 交互,我们会遇到需要设定时间段的情况(例如:timeout 参数).为了指定时间段,我们可以使用一个表示时间的 ...

  10. NEST 中的协变

    Convariant search results version 5.x NEST 直接支持返回协变结果集合.这意味着,可以将搜索结果的类型指定为一个接口或者基类,但是其真实类型仍然是接口或基类的一 ...

随机推荐

  1. Leaf——美团点评分布式ID生成系统

    背景 在复杂分布式系统中,往往需要对大量的数据和消息进行唯一标识.如在美团点评的金融.支付.餐饮.酒店.猫眼电影等产品的系统中,数据日渐增长,对数据分库分表后需要有一个唯一ID来标识一条数据或消息,数 ...

  2. java的多线程(一)

    我们知道我们打开个程序(或者说运行一款软件)其实也就是创建了一个进程,只不过程序是静态指令的集合,而进程是正在系统中运行的指令集合,进程是系统进行资源分配与调度的一个独立单位.进程具有独立性,动态性, ...

  3. unity3d 通过添加rigidBody来指明物体是动态的,以避免cache开销

    unity3d 通过添加rigidBody来指明物体是动态的,以避免cache开销. 如果不添加rigidBody,则unity会认为它是静态的,会对物理计算进行cache,但如果此物体实际上tran ...

  4. Andriod Studio中setText输出中文在AVD中显示乱码的解决方法

    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);      ...

  5. 如何进行SVN数据迁移并保存版本号数据

    如何从一台服务器192.168.8.2迁移到另一台服务器192.168.8.30进行SVN数据迁移并保存版本号数据 工具/原料   SVN 方法/步骤   1 打开远程服务,连接192.168.8.2 ...

  6. hdoj 1027 Ignatius and the Princess II 【逆康托展开】

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  7. 【转帖】漫话C++0x(四) —- function, bind和lambda

    实在是觉得此文总是去翻感觉不太好.于是果断转过来了,想看原文的请戳:http://www.wuzesheng.com/?p=2032 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lam ...

  8. 一款基于jQuery可放大预览的图片滑块插件

    今天给大家分享一款基于jQuery可放大预览的图片滑块插件.这款jQuery焦点图插件的特点是可以横向左右滑动图片,并且点击图片可以进行放大预览,唯一的缺陷是这款焦点图并不能循环切换,如果你有较好的J ...

  9. Hadoop的体系结构之HDFS的体系结构

    Hadoop的体系结构 Hadoop不仅是一个用于分布式存储的分布式文件系统,而是设计用来在由通用计算设备组成的大型集群上执行分布式应用的框架. HDFS和MapReduce是Hadoop中的两个最基 ...

  10. error LNK2019: 无法解析的外部符号 WinMain,该符号在函数 "int __cdecl invoke_main(void)”中被引用

    一,问题描述 MSVCRTD.lib(exe_winmain.obj) : error LNK2019: 无法解析的外部符号 WinMain,该符号在函数 "int __cdecl invo ...