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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值