嵌套类,摘自:
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. jenkins 批量修改配置文件

    jenkins 批量修改配置文件   jenkin job 修改配置 修改前配置 <runPostStepsIfResult> <name>FAILURE</name&g ...

  2. 如何在virtualenv虚拟环境中安装mysql-python

    接触过virtualenv后,想在这个虚拟环境中安装独立的开发环境.在安装MySQLdb时遇到错误 pc 09:09:30 File "/home/pc/work/VENV/py3/loca ...

  3. centos 6.5 文件目录管理

    Linux不存在像Windows那样分盘符的概念,Linux在安装之后就以文件目录的形式来进行管理,存储.即当我们安装完系统之后,我们就会看到有一堆的目录出现在根目录下.同时Linux使用正斜杠“/” ...

  4. k8s的容器监测探针

    大部分的应用程序我们在部署的时候都会适当的添加监控,对于运行载体容器则更应该如此.kubernetes提供了 liveness probes来检查我们的应用程序.它是由节点上的kubelet定期执行的 ...

  5. C#的MD5哈希值计算

    MD5哈希值计算:(仅仅是记录一下) /// <summary> /// 获取字符串的MD5值 /// </summary> /// <param name=" ...

  6. [sql]大型网站MySQL深度优化揭秘

    大型网站MySQL深度优化揭秘 第1章优化的思路和线路 1.1 网站优化的思路    2 1.2 MySQL优化,nginx这样的东西怎么优化? 第2章硬件层面优化 2.1 数据库物理机 2.1.1 ...

  7. 【Util】之——cookie

    拿走即用 使用前引入文件:http://files.cnblogs.com/ccto/util-cookie.js 使用方法: //设置cookie CookieUtil.set("name ...

  8. Django使用表单操作数据库

    前言 目标:实现Django通过表单的GET方式和POST方式提交数据,并添加到数据库 . OS:win10 x64 Django:1.11.8 Python: 3.6 本文完整示例:完整示例: 虽然 ...

  9. 程序挂在dynamic_cast<CCObject*>(pDelegate)->retain();

    CCTargetedTouchDelegate 的继承 和 dynamic_cast 想写个可以响应touch的sprite 类定义成了这个样子: class GemBoard : public CC ...

  10. python引用,浅拷贝,深拷贝

    1.引用 传递的是引用,原始列表改变,被赋值列表会同样改变,因为他们指向的是同一个地址. alist = [1,2,3,["a","b"]] blist = a ...