先提纲挈领地来个总结:

内部类(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. Jupyter notebook使用matplotlib不出图解决办法

    1.在jupyter notebook使用plot的时候没有显示图像2.在命令行知道需要使用ipython --pylab进入ipython环境才能做出图像,jupyter notebook该怎么设置 ...

  2. [Array]268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  3. 易语言连接RCON详细教程实例(演示连接Unturned服务器RCON)

    一.准备工作 工 具: 1.易语言 2.RCON服务端(这里我使用unturned服务器的RCON作为演示) 二.启动Unturned服务器并配置RCON 打开unturned服务器路径:F:\Unt ...

  4. 各NoSQL数据库管理系统与模型比较

    提供:ZStack云计算 内容简介 NoSQL数据库的存在意义在于提供传统关系数据库管理系统所不具备的特定功能.无论是负责承载简单的键-值对存储以实现短期缓存,抑或是处理传统数据库及结构化查询语言(简 ...

  5. java-Map-system

    一 概述 0--星期日1--星期一... 有对应关系,对应关系的一方是有序的数字,可以将数字作为角标. public String getWeek(int num){ if(num<0 || n ...

  6. uptime查看服务器运行时间

    uptime命令用于查看服务器运行了多长时间以及有多少个用户登录,快速获知服务器的负荷情况. uptime的输出包含一项内容是load average,显示了最近1,5,15分钟的负荷情况.它的值代表 ...

  7. Struts_客户列表练习

    1.导包 2.配置文件struts.xml 3.创建CustomerAction 4.修改menu.jsp和List.jsp 5.配置web.xml

  8. spring源码学习之bean的加载(一)

    对XML文件的解析基本上已经大致的走了一遍,虽然没有能吸收多少,但是脑子中总是有些印象的,接下来看下spring中的bean的加载,这个比xml解析复杂的多.这个加载,在我们使用的时候基本上是:Bea ...

  9. 【Codeforces Round #430 (Div. 2) B】Gleb And Pizza

    [链接]点击打开链接 [题意] 在这里写题意 [题解] 根据圆心到原点的距离这个东西判断一下圆在不在那个环里面就好 [错的次数] 0 [反思] 在这了写反思 [代码] #include <cst ...

  10. 转: PHP的"::"、"->"和"=>"的区别

    来自:http://blog.csdn.net/wgw335363240/article/details/20453379 1."::" 运算法 该运算符是调用一个类中的静态成员的 ...