博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nest(inner) class
阅读量:5256 次
发布时间:2019-06-14

本文共 4958 字,大约阅读时间需要 16 分钟。

嵌套类,摘自:        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.Framepublic 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.Framepublic 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   }}

 

转载于:https://www.cnblogs.com/helloworldtoyou/p/5203555.html

你可能感兴趣的文章
Linux重定向: > 和 &> 区别
查看>>
nginx修改内核参数
查看>>
C 筛选法找素数
查看>>
TCP为什么需要3次握手与4次挥手(转载)
查看>>
IOC容器
查看>>
Windows 2003全面优化
查看>>
URAL 1002 Phone Numbers(KMP+最短路orDP)
查看>>
web_day4_css_宽度
查看>>
electron入门心得
查看>>
格而知之2:UIView的autoresizingMask属性探究
查看>>
我的Hook学习笔记
查看>>
js中的try/catch
查看>>
寄Android开发Gradle你需要知道的知识
查看>>
简述spring中常有的几种advice?
查看>>
给你的网站404页面加上“宝贝寻亲”公益页面
查看>>
整理推荐的CSS属性书写顺序
查看>>
ServerSocket和Socket通信
查看>>
css & input type & search icon
查看>>
源代码的下载和编译读后感
查看>>
Kafka学习笔记
查看>>