zoukankan      html  css  js  c++  java
  • Nested Classes

    Note:

      A nested class is defined within another class.

    1 class OuterClass{
    2   ...
    3   class NestedClass{...}      
    4 }
    View Code

      There are two types of nested class. One is the static nested class, the other is no-static nested class(inner class).

    1 class OuterClass{
    2 
    3   ...
    4   static class StaticNestedClass{...}
    5 
    6   class InnerClass{...}
    7 
    8 }
    View Code

      A nested class is a member of outer class.

      Static nested classes can not access to outer class's members, While inner classes can do it.

    Why need Nested Classed?

      Group two related classes within one class is more logic,which makes pakages streamlined. Nested classes are "helper classes".

      Inrease encapsulation. Nested classes are hidden from the outside world.

      

    Static Nested Classes

      Like static class methods and variables, a static nested class is related to its outer class,

      which can not refer to its outer class members directly, but through an object reference of its outer class.

      Static nested classes like top-level classes for packaging convenience.

      The syntax of creating a instance of a static nested class in another class is as following

    1 OuterClass.StaticNestedClass  staticNestedClass = new OuterClass.StaticNestedClass()
    View Code

    Inner Classes

      Like instance methods and variables, an inner class is related to an instance of its outer class.

      So, an inner class can directly access its outer class members, but can not have static members itself.

      Instances of an inner class only exist within an instance of its outer class.

      For example

     1 public class OuterClass {
     2 
     3   private String name;
     4   public String getName(){return name;}
     5 
     6   // like a top-level class
     7   static class StaticNestedClass{
     8     // can own static members
     9     static int age;
    10     static void getMessage(){}
    11 
    12     void test(){
    13 
    14       // access outer class members through a object reference of outer class
    15       OuterClass outerClass = new OuterClass();
    16       outerClass.name = outerClass.getName();
    17       outerClass.name = "outer class name";
    18     }
    19   }
    20 
    21   // like an instance method or variable of its outer class
    22   class InnerClass{
    23     // can not have static members
    24 //    static String size;
    25 
    26     void test(){
    27 
    28       // can directly access outer class members
    29       name = getName();
    30       name = "outer class name";
    31     }
    32   }
    33 
    34   public static void main(String[] args) {
    35 
    36     // syntax of creating a static nested class within another class.
    37     OuterClass.StaticNestedClass staticNestedClass = new OuterClass.StaticNestedClass();
    38 
    39     // syntax of creating a inner class within another class.
    40     OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();
    41 
    42   }
    43 }
    View Code

     reference from: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

      

      

      

  • 相关阅读:
    javascript,函数声明和函数表达式
    javascript,小数值舍入操作方法:ceil()、floor()、round()
    javascript,子字符串操作方法:Slice()、Substr()、Substring()的区别
    javascript,第一个基于node.js的Http服务
    javascript,创建对象的3种方法
    MFC学习笔记2——MFC和Win32
    Qt下 QString转char*
    [转载] Qt程序在Windows下的mingw发布
    VC 获取当前时间
    MFC 对话框设计问题(控件的使用)
  • 原文地址:https://www.cnblogs.com/yangwu-183/p/13479458.html
Copyright © 2011-2022 走看看