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

      

      

      

  • 相关阅读:
    前端导出excel文件
    promise和async/await的用法
    vue element 导出 分页数据的excel表格
    mac net.core 安装问题总结
    npm报MSBUILD错误的解决办法
    现大前端开发环境配置
    git 常用命令
    NodeJs (一)
    VUE 组件通信、传值
    vue-cli 第一章
  • 原文地址:https://www.cnblogs.com/yangwu-183/p/13479458.html
Copyright © 2011-2022 走看看