Lei 的个人资料wolfshow照片日志列表更多 工具 帮助

日志


5月10日

JAVA第1记

一、对象的构造顺序:
1.static method 或者 static field被访问,同时寻找.class文件
2..class文件被load,创建Class对象,static initializers工作且仅工作一次。
3.heap上为这个对象分配空间
4.刚刚分配的这片空间清零,为所有primitives赋默认值,references为null
5.field definition for non-static执行
6.constructors are executed.
二、设计模式之delegation
有些人认为composition+forwarding即为delegation,包括我正在阅读的Thinking in Java 4th,不知道Bruce Eckel本人是怎样理解的,不过根据若干本design pattern资料的介绍(包括GOF在内),都认为上述观点是一种误解,delegation定义如下:
Delegation
Delegation is a way of making composition as powerful for reuse as inheritance [Lie86, JZ91]. In delegation, two objects are involved in handling a request: a receiving object delegates operations to its delegate. This is analogous to subclasses deferring requests to parent classes. But with inheritance, an inherited operation can always refer to the receiving object through the this member variable in C++ and self in Smalltalk. To achieve the same effect with delegation, the receiver passes itself to the delegate to let the delegated operation refer to the receiver
例子代码如下:
  1. part2  
  2.   
  3. delegateA {  
  4.     delegateeB b;  
  5.   
  6.      void methodA() { b.methodB(this); }     
  7.      void  do() {}  
  8. }  
  9.   
  10. delegateeB {  
  11.      void methodB(delegateA a) { a.do(); }  
  12. }  
 delegate已经把自身pass给了delegatee,并且delegatee调用了delegate的方法,这就是一种indirection。

在现实世界中,假如董事长A把权利授权给总经理B,总经理B一定会获取董事长A才拥有的权利,它会利用这些权利来替公司做事。这才是真正的 delegation,也就是说delegatee一定会调用delegate的某些方法,因此你首先得把delegate传递给delegatee。

如果有人对下列问题感兴趣可以回答,可以给我留言并回答

在JAVA中,x和i为两个变量,什么时候x=x+i是合法表达式,而x+=i不是?而在什么时候x+=i是合法表达式,而x=x+i不是?在这里合法表达式指的是能够通过编译。

5月9日

开新类别了

其实学习JAVA有一段时间了,小程序算不上软件的也写过几个,也总有一些心得体会,但是不写下来就会忘记,最近开始一次JAVA底层全方位学习,为了把科技创新做的不俗套,也就是说能看出创新,争取每天记录一些心得(不过不一定能做到),林信良也是一点一点做到的,人家都写了两本书了,所以就像他学习了,把点点滴滴记录下来,自己回顾起来也方便。