nest(inner) class
嵌套类,摘自:
http://www.ntu.edu.sg/home/ehchua/programmin/java/J4a_GUI.html
A nested class has these properties:
- 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
newoperator and constructor. - A nested class is a member of the outer class, just like any member variables and methods defined inside a class.
- Most importantly, a nested class can access the
privatemembers (variables/methods) of the enclosing outer class, as it is at the same level as theseprivatemembers. This is the property that makes inner class useful. - A nested class can have
private,public,protected, or the default access, just like any member variables and methods defined inside a class. Aprivateinner class is only accessible by the enclosing outer class, and is not accessible by any other classes. [An top-level outer class cannot be declaredprivate, as no one can use aprivateouter class.] - A nested class can also be declared
static,finalorabstract, just like any ordinary class. - 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:
- To control visibilities (of the member variables and methods) between inner/outer class. The nested class, being defined inside an outer class, can access
privatemembers of the outer class. - 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.
- 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的更多相关文章
- JSONModel 嵌套字典数组 JSONModel nest NSDictionary NSArray
JSONModel 嵌套字典数组 JSONModel nest NSDictionary NSArray
- Nest查询示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- NEST.net Client For Elasticsearch简单应用
NEST.net Client For Elasticsearch简单应用 由于最近的一个项目中的搜索部分要用到 Elasticsearch 来实现搜索功能,苦于英文差及该方面的系统性资料不好找,在实 ...
- Elasticsearch .Net Client NEST使用说明 2.x
Elasticsearch .net client NEST使用说明 2.x Elasticsearch.Net与NEST是Elasticsearch为C#提供的一套客户端驱动,方便C#调用Elast ...
- Elasticsearch .Net Client NEST 多条件查询示例
Elasticsearch .Net Client NEST 多条件查询示例 /// <summary> /// 多条件搜索例子 /// </summary> public c ...
- Elasticsearch .Net Client NEST 索引DataSet数据
NEST 索引DataSet数据,先序列化然后转成dynamic 类型进行索引: /// <summary> /// 索引dataset /// </summary> /// ...
- nest 'for' loop.
/* nest for loop demo. Note that,'upside' triangle controls 'inner condition'. */ import kju.print.P ...
- Nest客户端的基本使用方法
通过Nuget安装好Nest的相关Dll,之后我们就可以开始了, 1.初始化Nest客户端 string indexName = "customer"; Uri uri = new ...
- NEST 中的时间单位
Time units 英文原文地址:Time units 与 Elasticsearch 交互,我们会遇到需要设定时间段的情况(例如:timeout 参数).为了指定时间段,我们可以使用一个表示时间的 ...
- NEST 中的协变
Convariant search results version 5.x NEST 直接支持返回协变结果集合.这意味着,可以将搜索结果的类型指定为一个接口或者基类,但是其真实类型仍然是接口或基类的一 ...
随机推荐
- tomcat6的编译和导入myeclipse
声明:近期在学习tomcat6的源代码,网上搜索了些相关的资料,并自己操作了下进行了对应的汇总.如今总结例如以下 本文目的:编译tomcat6源代码+导入tomcat6源代码到myeclipse 測试 ...
- vim设置文本宽度
'textwidth' 'tw' number (default 0) local to buffer ...
- winphone开发环境配置
环境:操作系统win7 要进行winphone开发,必须进行一些环境的配置.下面是我的一些配置总结. 1.操作系统 winphone开发仅仅能在win8下开发.所以首先得安装win8.能够使用nt6 ...
- Markdown 11种基本语法【转】
[转自:http://www.cnblogs.com/hnrainll/p/3514637.html] 1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置标题,有两种方式 ...
- poi 读取数据处理方式
poi读取数据的时候空格,字符数据,数字类型数据处理方式 logger.info("============ExeclReader.readExeclToMapList() begin=== ...
- 在django中访问静态文件(js css img)
刚开始参考的是别的文章,后来参考文章<各种 django 静态文件的配置总结>才看到原来没有但是没有注意到版本,折腾了一晚上,浪费了很多很多时间.后来终于知道搜索django1.7访问静态 ...
- eclipse下properties配置文件中文乱码解决
properties文件常带有中文注释,eclipse显示是乱码. 安装插件(properties editor)可以解决properties配置文件乱码的问题. 菜单 : Help->Ecli ...
- c# 操作Word总结【转】
http://www.cnblogs.com/eye-like/p/4121219.html 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很 ...
- 一款纯css3实现的动画加载导航
之前为大家介绍了好几款导航菜单,今天为给大家再带来一款纯css3实现的动画加载导航.该导航出现的时候以动画的形式出现.效果图如下: 在线预览 源码下载 实现的代码. html代码: <ul ...
- python基础系列教程——Python中的编码问题,中文乱码问题
python基础系列教程——Python中的编码问题,中文乱码问题 如果不声明编码,则中文会报错,即使是注释也会报错. # -*- coding: UTF-8 -*- 或者 #coding=utf-8 ...