美文网首页
[读书笔记] Think in Java r_8

[读书笔记] Think in Java r_8

作者: 十旋转45度 | 来源:发表于2017-06-01 10:59 被阅读0次

P108 finalize()

The odd thing about this is that you call gc( ) before you call runFinalization( ), which seems to contradict the JavaSoft documentation which claims that finalizersare run first, and then the storage is released. However, if you call runFinalization( ) first, and then gc( ), the finalizers will not be executed.

P131 protected

同一个包内的所有类可以访问,异包的子类也可以访问

P183 static inner class

To understand the meaning of static when applied to inner classes, you must keep in mind the idea that the object of the inner class implicitly keeps a handle to the object of the enclosing class that created it. When you say an inner class is static, this is not true, which means:

  1. You don’t need an outer-class object in order to create an object of a static inner class.
  2. You can’t access an outer-class object from an object of a static inner class.

P183 inheriting from inner classes

Because the inner class constructor must attach to a handle of the enclosing class object, things are slightly complicated when you inherit from an inner class. The problem is that the “secret” handle to the enclosing class object must be initialized, and yet in the derived class there’s no longer a default
object to attach to. The answer is to use a syntax provided to make the association explicit:

//: InheritInner.java
// Inheriting an inner class
class WithInner {
class Inner {}
}
public class InheritInner
extends WithInner.Inner {
//! InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String args[]) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
} ///:~

You can see that InheritInner is only extending the inner class, not the outer one. But when it comes time to create a constructor, the default one is no good and you can’t just pass a handle to an enclosing object. In addition, you must use the syntax

enclosingClassHandle.super();

inside the constructor. This provides the necessary handle and the program will then compile.

P240 try,finally,Exception

//: LostMessage.java
// How an exception can be lost
class VeryImportantException extends Exception {
    public String toString() {
        return "A very important exception!";
    }
}
class HoHumException extends Exception {
    public String toString() {
        return "A trivial exception";
    }
}
public class LostMessage {
    void f() throws VeryImportantException {
        throw new VeryImportantException();
    }
    void dispose() throws HoHumException {
        throw new HoHumException();
    }
public static void main(String args[]) throws Exception {
    LostMessage lm = new LostMessage();
    try {
        lm.f();
     } finally {
        lm.dispose();
     }
    }
} ///:~

The output is:

A trivial exception
at LostMessage.dispose(LostMessage.java:21)
at LostMessage.main(LostMessage.java:29)

You can see that there’s no evidence of the VeryImportantException, which is simply replaced by the
HoHumException in the finally clause. This is a rather serious pitfall, since it means an exception can be completely lost, and in a far more subtle and difficult-to-detect fashion than the example above. In contrast, C++ treats the situation where a second exception is thrown before the first one is handled as a dire programming error. Perhaps a future version of Java will repair the problem (the above results were produced with Java 1.1).

P243 Exception Caught order

When an exception is thrown, the exception-handling system looks through the “nearest” handlers in
the order they are written.
If you try to “mask” the derived-class exceptions by putting the base-class catch clause first, like this:

try {
    throw new Sneeze();
} catch(Annoyance e) {
    System.out.println("Caught Annoyance");
} catch(Sneeze d) {
    System.out.println("Caught Sneeze");
}

The compiler will give you an error message, since it sees that the Sneeze catch-clause can never be reached.

相关文章

  • [读书笔记] Think in Java r_8

    P108 finalize() The odd thing about this is that you cal...

  • 读书笔记 | 《Think in Java》Ⅱ一切都是对象

    上篇:读书笔记 | 《Think in Java》前言&Ⅰ对象导论 Ⅱ. 一切都是对象 ---4.4更新--- 2...

  • 读书笔记 | 《Think in Java》Ⅻ 并发

    并发编程的两大优点:1. 程序执行速度得到提升。2. 为设计某些类型的程序提供更易用的模型。 并发“具有可论证的确...

  • 读书笔记 | 《Think in Java》Ⅷ 多态

    Ⅷ 多态 多态主要将做什么和怎么做分离开来,从而改善了代码的组织结构和可读性。多态可以帮助你创建可拓展的程序,在开...

  • 读书笔记 | 《Think in Java》Ⅸ 接口

    Ⅸ 接口 9.1抽象类和抽象方法 抽象类是为了建立一种基本形式,以此表示所有导出类的公共部分。创建一个抽象类的对象...

  • Think in Java

    类描述了具有相同特性和行为的对象的集合。所以类实际上是一个数据类型。 public表示任何人都可以使用privat...

  • Think in Java

    一切皆对象 引用 每种编程语言都有操纵内存中元素的方式。例如C/C++之于指针、Java之于引用把对象看作电视机(...

  • OOP

    java in think 起因 自从买了Java in think 这本书,一直想认真仔细的品味下这本某种意义的...

  • 读书笔记 | 《Think in Java》Ⅶ 复用类

    Ⅶ 复用类 复用代码是Java众多引人注目的功能之一,Java的代码复用都是围绕类来展开的。所以很多时候,不需要重...

  • 读书笔记 | 《Think in Java》Ⅺ 持有对象

    Ⅺ 持有对象 大部分情况下,对象的存在数量和生命周期都是未知的,程序总是根据运行时才能得到的某些条件去创建新对象。...

网友评论

      本文标题:[读书笔记] Think in Java r_8

      本文链接:https://www.haomeiwen.com/subject/xhbbxxtx.html