`

Java Puzzler

阅读更多

2011.03.24

now i've been through 81 puzzles. every puzzle, i've read it as carefully and detailedly as possible, the next is the 82nd, Beer Blast.


2011.03.27

now i've arrived at the puzzle 90.

 

The snippet given is the following:

 

public class Outer {   
    class Inner1 extends Outer  {}   
    class Inner2 extends Inner1 {}
}
// DOES NOT COMPILE!!

 

The problem is that due to how default constructor is defined, we really have the following:

// Same as above but with default constructor included explicitly
public class Outer {
    class Inner1 extends Outer  { 
        Inner1() { super(); }
    }
    class Inner2 extends Inner1 {
        Inner2() { super(); }    
    }
}
// STILL DOES NOT COMPILE!!

 

The problem is that Inner2 's superclass is itself an inner class Inner1 , thus making Inner2 's default constructor illegal since it requires an enclosing instance to be supplied to the constructor.

The "brute-force" way to fix the problem is to provide this explicitly with a qualified-this expression:

 

// "brute-force" fix
public class Outer {
    class Inner1 extends Outer  { 
        Inner1() { super(); }
    }
    class Inner2 extends Inner1 {
        Inner2() { Outer.this.super(); }    
    }
}
// NOW COMPILES!

 

However, the puzzle prescribes that such complicated situation is best avoided in the first place. Here are some quotes:

 

This compiles, but it is mind-numbingly complex. There is a better solution: Whenever you write a member class, ask yourself, Does this class really need an enclosing instance? If the answer is no, make it static . Inner classes are sometimes useful, but they can easily introduce complications that make a program difficult to understand. They have complex interactions with generics (Puzzle 89), reflection (Puzzle 80), and inheritance (this puzzle). If you declare Inner1 to be static , the problem goes away. If you also declare Inner2 to be static , you can actually understand what the program does: a nice bonus indeed.

In summary, it is rarely appropriate for one class to be both an inner class and a subclass of another. More generally, it is rarely appropriate to extend an inner class; if you must, think long and hard about the enclosing instance. Also, prefer static nested classes to non-static . Most member classes can and should be declared static .

 

http://stackoverflow.com/questions/3383460/odd-situation-for-cannot-reference-this-before-supertype-constructor-has-been-ca

 

2011.03.28

today, i've arrived at puzzle 94: Lost in the shuffle, this one really made me suffer a lot from deep thoughts, cuz in the first place, i didn't understand the following paragraph:

 

Why does the graph have this shape? We don't know all the details but we can offer some intuition. Let's restrict our attention to the array's first element. After the first iteration of the loop, it has the correct expected value of (n - 1) / 2. In the second iteration, however, there is 1 chance in n that the random number generator will return 0 and the value in the first element will be replaced by 1 or 0. In other words, the second iteration systematically reduces the expected value of the first element. In the third iteration, there's a further 1 chance in n that the first element is replaced by 2, 1, or 0, and so on. For the first n / 2 iterations of the loop, the expected value of the first element decreases. For the second n / 2 iterations, it increases but never catches up to its fair value. Note that the last element in the array is guaranteed to have the correct expected value, as the last step in the execution of the method is to select it at random from all the elements in the array.

 

why after second iteration, the first element will be replaced by 1 or 0, where does the 0 come from? yes, it comes from the first iteration, if the randomly generated number of the first iteration is 1, a[0] and a[1] are exchagned, thus a[1] has been replaced by 0. great!

 

2011.03.29

i've finished it.

分享到:
评论

相关推荐

    java puzzler (java谜题)

    《Java Puzzler》是Java开发者的一本独特指南,它以一种有趣且富有挑战性的方式揭示了语言中的陷阱和易犯错误。这本书的核心理念是通过一系列精心设计的谜题,帮助读者深入理解Java语言的微妙之处,从而提高编程技能...

    Javapuzzler+JAVA解惑 中英双语 完整源代码

    《Javapuzzler+JAVA解惑 中英双语 完整源代码》是一份集学习与实践于一体的Java编程资源,包含《Java Puzzlers》和《JAVA解惑》两部分,以及对应的完整源代码。这份资料对于深入理解Java语言的特性和陷阱,提升编程...

    JavaPuzzler.pdf

    不错的好东西 一个个小题目 非常关注细节

    Java解惑(javapuzzler.chm)

    Java的优秀图书,欢迎下载。

    java-puzzler:学习Java Puzzler

    《Java Puzzler》是一本深受Java开发者喜爱的书籍,它揭示了Java语言中的一些微妙陷阱和不明显的特性,帮助程序员避免在实际编程过程中遇到的困惑。这些“Puzzlers”通常是由看似简单但实则暗藏玄机的代码片段构成,...

    java puzzler code

    Java Puzzlers是Java编程中的一系列巧妙问题,旨在揭示语言中的陷阱、误导性和意外行为。这些谜题通常由简单的代码片段构成,看似无害,但执行结果却往往出乎意料。通过解决这些Puzzlers,开发者可以增强对Java语言...

    java解惑(java谜题)中文版的

    《Java解惑》是一本专为Java程序员设计的书籍,旨在揭示编程中常见的陷阱、误解和易犯的错误。这本书的中文版使得更多的中国开发者能够深入理解这些“谜题”,提高编程技能。Java Puzzlers是由Java之父James Gosling...

    java puzzlers code

    例如,一个经典的Java Puzzler是关于变量初始化的。在Java中,局部变量必须先初始化后才能使用,但有时候编译器可能会做出一些你意想不到的优化。如果在函数内部声明并初始化一个变量,然后在后续的代码块中再次初始...

    java puzzle

    ### Java Puzzler: The Joy of Sets #### Introduction 在探讨Java编程中可能出现的各种奇怪行为时,"Java Puzzlers"系列提供了一系列短小精悍的示例代码,旨在揭示那些初看之下难以理解的行为背后的原因。这些...

    Java解惑.中文完整版

    This lively book reveals oddities of the Java programming language through entertaining and thought-provoking programming puzzles." --Guy Steele, Sun Fellow and coauthor of The Java(t) Language ...

    Java解惑PPT8

    Java编程语言中有许多微妙而有趣的细节,这些细节可能会在开发过程中造成困扰,这就是"Java解惑"系列试图解决的问题。本文将深入探讨PPT8中提及的三个Java谜题:Puzzle 76 乒乓、Puzzle 77 搞乱锁的妖怪和Puzzle 78 ...

    Java+Puzzlers(中英文并且带源码)

    每个Puzzler都是一次深入学习的机会,通过解决这些谜题,开发者不仅可以提升对Java语言的理解,还能增强代码调试和问题解决的能力。书中附带的源代码可以方便读者直接运行和测试,加深对知识点的理解。无论你是Java...

    Java puzzlers(java 解惑)附源码

    例如,puzzler之一可能涉及自动装箱和拆箱,当Integer对象与int基本类型进行操作时,如果不理解它们之间的关系,可能会出现意料之外的结果。源码中可能包含这样的例子,展示了如何正确处理这种情况,避免隐式转换...

    java Puzzlers 中文版带完整目录

    本書包含了Java程式語言和核心函式庫中的各種謎題,任何具備使用Java經驗的讀者都可以看得懂,但是書裡有不少謎題的難度頗高,即便是對經驗豐富的Java程式設計師而言,都是一項挑戰,所以如果你解不出來,別覺得難過...

    <好书>java解惑(java puzzlers),过来挑战吧

    每个puzzler都会引导读者思考为什么这段代码会以某种意想不到的方式运行,然后提供详细的解答,解释背后的语言原理。这些谜题涵盖了从基本语法到高级特性的各种主题,包括但不限于类型转换、对象引用、内存管理、多...

    Java解惑PPT7

    Java编程中的迷惑问题,或者称为Puzzlers,是学习Java时常常遇到的陷阱和微妙之处。这些Puzzlers有助于深入理解Java的语法规则和运行机制。以下将详细解析给出的Puzzles。 首先,我们来看Puzzle 66:一件私事。这个...

    Java解惑ppt5

    《Java解惑PPT5》深入探讨了Java编程中的一些常见困惑和陷阱,特别是关于类、构造器重载以及静态域的使用。以下是对其中两个关键Puzzle的详细解析: **Puzzle 46:令人混淆的构造器** 在这个谜题中,作者展示了...

    Java解惑 PPT1

    【Java解惑PPT1】深入探讨Java编程中的常见迷惑 在Java编程中,了解语言的细微之处至关重要,因为这可能会导致意想不到的行为。本PPT主要涵盖了五个有趣的Java谜题,帮助开发者理解Java的一些核心概念。 **谜题1:...

    Java解惑ppt4

    Java解惑是本很不错的书,讲解了Java中一些很有趣的东西

    Java解惑PPT6

    在Java编程中,有一些微妙而重要的知识点,这些知识点在实际编程中可能会引起混淆或错误。在"Java解惑PPT6"中,我们探讨了几个关键的Java特性,特别是关于不变性、equals()和hashCode()方法的约定以及它们在HashSet...

Global site tag (gtag.js) - Google Analytics