摘要: 1. 构造代码块会被加入到每一个构造函数中2.例外情况:当构造函数中又调用了this()构造函数的话,则不会再调用,即此时实际上只调用了一次构造代码块。eg:class Base(){{ System.out.println("Constructor");} Base(){} Base(String i){ this();}} 阅读全文
posted @ 2013-12-09 09:10 akingseu 阅读(90) 评论(0) 推荐(0) 编辑
摘要: 容易引起循环和堆栈运行溢出。class Father{ Father(){new Other();}}class Son extends Father{ Son(){}}class Other(){ Other(){ new Son();}} 阅读全文
posted @ 2013-12-09 08:56 akingseu 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 通过这个例子,可以分析一下 父类 子类的变量的初始化过程:publicabstractclassServer{publicfinalstaticintDEFAULT_PORT=40000;publicServer(){intport=getPort();System.out.print(port);}protectedabstractintgetPort();}publicclassSimpleServerextendsServer{privateintport=100;publicSimpleServer(intport){this.port=port;}protectedintgetPor 阅读全文
posted @ 2013-12-09 08:51 akingseu 阅读(200) 评论(0) 推荐(0) 编辑
摘要: 覆写是针对非静态方法,不能针对静态方法。publicclassBase{publicstaticvoiddoSth(){System.out.println("Base:Sth");}publicvoiddoAnyth(){System.out.println("Base:Any");}}publicclassSubextendsBase{publicstaticvoiddoSth(){System.out.println("Sub:Sth");}publicvoiddoAnyth(){System.out.println(" 阅读全文
posted @ 2013-12-09 08:31 akingseu 阅读(191) 评论(0) 推荐(0) 编辑
摘要: publicclassClient{publicstaticinti=1;static{i=100;}publicstaticvoidmain(String[]args){System.out.println(i);}}//i = 100publicclassClient{static{i=100;}publicstaticinti=1;publicstaticvoidmain(String[]args){System.out.println(i);}}//i = 1奇怪,为啥能通过编译呢?静态变量是类加载时被分配到数据区的,它在内存中只有一个拷贝,不会被分配多次,后面的所有赋值操作都是值改变 阅读全文
posted @ 2013-12-09 08:11 akingseu 阅读(238) 评论(0) 推荐(0) 编辑
摘要: 建议31:在接口中不要存在实现代码publicinterfaceIB{publicstaticfinalISs=newIS(){publicvoiddoSth(){System.out.println("我在接口中实现了");}};}publicinterfaceIS{publicvoiddoSth();}publicclassClient{publicstaticvoidmain(String[]args){IB.s.doSth();}}//结果打印出,我在接口中实现了IB接口中声明了一个静态常量s,其值是一个匿名内部类,实现了接口IS.这是一种不好的实现,接口是一种契约 阅读全文
posted @ 2013-12-09 07:52 akingseu 阅读(223) 评论(0) 推荐(0) 编辑