先提纲挈领地来个总结:

内部类(Inner Class)有四种:

  1. member inner class,即成员内部类。可以访问外部类所有方法与成员变量。生成成员内部类对象的方法:OuterClass.InnerClass inner = new OuterClass().new InnerClass();
  2. static inner class,即静态内部类。只能访问外部类的静态方法与静态成员变量,生成静态内部类对象的方法:OuterClass.InnerClass inner = new OuterClass.InnerClass();
  3. Local Inner Class,即局部内部类。定义在方法中,只能访问方法中声明的final 类型的变量(如果作为构造体参数,则不必是final)。
  4. Anonymous Inner Class,即匿名内部类。匿名内部类会隐式地继承一个父类或实现一个接口。

个人认为,内部类最常见的应用场景是:在方法中返回匿名内部类对象。


Remember the advice: Prefer classes to interfaces. If your design demands an interface, you'll know it. Otherwise, don't put it in ultil you are forced to.

  • It's possible to place a class definition within another class definition. This is called an inner class.
  • when you create an inner class, an object of that inner class has a link to the enclosing object that made it, and so it can access the members of that enclosing object--without any special qualifications. In addition, inner classes have access rights to all the elements(include private member) in the enclosing class. How can this happen? The inner class secretly captures a reference to the particular object of the enclosing class that was responsible for creating it. Then, when you refer to a member of the enclosing class, the reference is used to select that member. Fortunately, the compiler takes care of all these details for you.
  • 在内部类中通过外部类名.this 可以拿到外部类对象的引用。
  • 外部类对象.new 语法可以用于创建外部类的内部类对象。
  • It's not possible to create an object of the inner class unless you already have an object of the outer class. This is because the object of the inner class is quietly connected to the object of the outer class that is was made from. However, if you make a nested class(a static inner class), then it doesn't need a reference to the out-class object.
  • Normal(non-inner) classes cannot be made private or protected; they may only be given public or package access.
  • Inner classes can be created within a method or even an arbitrary scope. There are two reasons for doing this: 1. You're implementing an interface of some kind so that you can create and return a reference. 2. You're solving a complicated problem and you want to create a class to aid in your solution, but you don't want it publicly available.
  • Anonymous inner classes are somewhat limited compared to regular inheritance, because they can either extend a class or implement an interface, but not both. And if you do implement an interface, you can only implement one.
  • To understand the meaning of static when applied to inner classes, you must remember that the object of an ordinary inner class implicitly keeps a reference to the object of the enclosing class that created it. This is not true, however, when you say an inner class is static. A static inner class means: 1. You don't need an outer-class object in order to create an object of a nested class. 2. You can't access a non-static outer-class object from an object of a static inner class.
  • Ordinary inner classes cannot have static data, static fields, or static inner classes. However, static inner classes can have all of these.
  • As you've see earlier, in an ordinary(non-static) inner class, the link to the outer-class object is achieved with a special this reference. A static inner class does not have a special this reference, which makes it analogous to a static method.

TIJ——Chapter Ten:Inner Classes的更多相关文章

  1. TIJ——Chapter Seven:Reusing Classes

    Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has ...

  2. Journal entry of the eighth chapter to chapter ten

    Chapter eight: 当我们做一个项目的时候,一开始可能会信息满满,或者说是通过一些调查分析后觉得自己的团队能完全实现用户所提出的所有要求,但是,往往在很自信的时候,我们都会处处碰壁,因为组内 ...

  3. TIJ——Chapter One:Introduction to Objects

    ///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...

  4. TIJ——Chapter Eleven:Holding Your Objects

    Java Provides a number of ways to hold objects: An array associates numerical indexes to objects. It ...

  5. TIJ——Chapter Two:Everything Is an Object

    If we spoke a different language, we would perceive a somewhat different world. Ludwig Wittgenstein( ...

  6. Chapter 3 Discovering Classes and Object

    Chatper 3 Discovering Classes and Object Exercises: 1.What is a class? A class is a template for man ...

  7. TIJ——Chapter Fourteen:Type Information

    Runtime type information(RTTI) allows you to discover and use type information while a program is ru ...

  8. TIJ——Chapter Twelve:Error Handling with Exception

    Exception guidelines Use exceptions to: Handle problems at the appropriate level.(Avoid catching exc ...

  9. TIJ——Chapter Nine:Interfaces

    A class containing abstract methods is called an abstract class. If a class Contains one of more abs ...

随机推荐

  1. T2487 公交司机(搜索题)(小L的一生)

    https://www.luogu.org/problem/show?pid=T2487 题目背景 小L那没出息的儿子当上了一个公交司机. 题目描述 每个司机都有一个牌子,牌子的正面标出了这个司机所开 ...

  2. Jdbc封装和对CURD的封装

    1.查询emp表中的所有记录为例 2.测试类 public Emp getByNameAndEmail(String name, String email){ String sql = "s ...

  3. PHP的安全性问题,你能说得上几个?

    一.SQL注入 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具体来说,它是利用现有应用程序,将(恶意)的SQL命令 ...

  4. java -cp ../../DESUtil/ Hello,用-cp指定classpath

    运行hello.class 文件 怎么用 java +路径 就是不在class目录下运行 怎么做??? 我想要的是 java 直接去运行某个路径下的class文件 不想到它的目录那里再java hel ...

  5. 对BOM的总结

    参考:JavaScript半知半解 TG著 BOM对象 Window对象是客户端JavaScript程序的全局对象. Window对象使得JavaScript与浏览器进行交互. 所有的JavaScri ...

  6. CesiumLab V1.4 分类3dtiles生成(倾斜单体化、楼层房间交互)我记得我是写过一篇关于倾斜单体化的简书文章的,但是现在找不到了。不过找不到也好,就让他随风逝去吧,因为当时我写那篇文章的时候,就发现了cesium实际是有另一种更高效的单体化。就下面这个示例https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/index.html?src=

    我记得我是写过一篇关于倾斜单体化的简书文章的,但是现在找不到了.不过找不到也好,就让他随风逝去吧,因为当时我写那篇文章的时候,就发现了cesium实际是有另一种更高效的单体化.就下面这个示例 http ...

  7. GIT生成公钥和私钥

    转载至:https://blog.csdn.net/gwz1196281550/article/details/80268200 打开 git bash! git config --global us ...

  8. java-日期类

    一 显示系统时间 package cn.itcast.api.a.date; import java.text.DateFormat; import java.util.Date; public cl ...

  9. free内存监控

    语 法: free [-bkmotV][-s <间隔秒数>] 补充说明:free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存区段,以及系统核心使用的缓冲区等. 参 ...

  10. 洛谷P2723 丑数 Humble Numbers [2017年 6月计划 数论07]

    P2723 丑数 Humble Numbers 题目背景 对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S.这个正整数集合包括, ...