zoukankan      html  css  js  c++  java
  • Java

    (本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

    Nested Classes

    class OuterClass {
        ......
        class StaticNestedClass {
            ......
        }
        ......
        class InnerClass {
        ......
    }

    定义

    nested classes (嵌套类)分为两种:non-staticstatic,前者通常称作inner classes,后者称作static nested classes

    作用域与访问范围

    两种nested classes都是其enclosing class的内部成员,non-static classes可以引用到包装类的private成员,static classes没有这个能力。两种nested classes都可以被声明为public/protected/private/package private

    作用场景(因原文概括性高,直接引用)

    • It is a way of logically grouping classes that only used in one place.
    • It increases encapsulation.
    • It can lead to more readable and maintainable code.

    Static Nested Classes

    • static nested classes不可直接调用其外部类的方法/成员,必须通过一个外部类的实例才能访问(在这一点上,static inner class表现的如同一个top-level class
    • 用如下方式创建一个static nested class实例
      OuterClass.StaticNestedClass foo = new OuterClass.StaticNestedClass();

    Inner Classes

    • As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields.
    • Cause an inner class is associated with an instance, it cannot define any static members itself.
    • An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
    • 用如下方式创建一个inner class实例
      OuterClass.InnerClass innerObject = outerObject.new InnerClass();
    • 有两种特殊的inner classes:local classesanonymous class

    Shadowing(不知确切的中文翻译是什么,“遮蔽”?)

    原文用一段很简洁的代码讲清了这个问题

    public class Main {
        public static void main(String[] args) throws Exception {
            (new OuterClass()).new InnerClass().printX(2);
            System.out.println("FINISH!");
        }
    }
    
    class OuterClass {
        int x = 0;
        class InnerClass {
            int x = 1;
            public void printX (int x) {
                System.out.println("x=" + x);
                System.out.println("this.x=" + this.x);
                System.out.println("OuterClass.this.x=" + OuterClass.this.x);
            }
        }
    }

    输出:

    x=2
    this.x=1
    OuterClass.this.x=0
    FINISH!

    Serialization

    • Serialization of inner classes, including local and annonymous classes, is strongly discouraged.
    • 原因是编译器在compile certain constructs时,会产生synthetic constructs:these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code.
    • You may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation.
  • 相关阅读:
    基于GIS的生态环境信息一张图系统应用与研究
    基于GIS的开源社区(村)网格化数据管理平台
    智慧乡村综合解决方案数字化智能农业
    开源免费乡村振兴数字乡村平台
    Oracle连接字符串记录
    SqlLocalDB使用笔记
    Orchard分类Taxonomies图文教程
    自己手动maven命令将jar包安装到本地。
    Mysql如何去掉数据库中重复记录?
    常见的数据库方面问题
  • 原文地址:https://www.cnblogs.com/maozhige/p/4063596.html
Copyright © 2011-2022 走看看