重读《Java编程思想》
相关最新代码已上传至我的GitHub了(https://github.com/WenyangSun/ThinkingInJava),后续例子没有在博客上更新。
1、在类的内部,变量定义的先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间,它们仍旧会在任何方法(包括构造器)被调用之前得到初始化。
package com.ietree.base.initialization; // 在类的内部,变量定义的先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间,它们仍旧会在任何方法(包括构造器)被调用之前得到初始化。
class Window
{
Window(int maker)
{
System.out.println("Window(" + maker + ")");
}
} class House
{
House()
{
System.out.println("House()"); //
w3 = new Window(33); //
} Window w1 = new Window(1); // Window w2 = new Window(2); // void f()
{
System.out.println("f()"); //
Window w4 = new Window(4); //
} Window w3 = new Window(3); //
} // output:
// Window(1)
// Window(2)
// Window(3)
// House()
// Window(33)
// f()
// Window(4)
public class OrderOfInitialization
{
public static void main(String[] args)
{
House h = new House();
h.f(); //
}
}
2、初始化的顺序是先静态对象,而后是“非静态”对象,构造器可以看成是静态方法。
package com.ietree.base.staticintialization; /**
* 初始化的顺序是先静态对象,而后是“非静态”对象,构造器可以看成是静态方法。
*/
class Bowl
{
public Bowl(int marker)
{
System.out.println("Bowl(" + marker + ")");
} void f1(int marker)
{
System.out.println("f1(" + marker + ")");
}
} class Table
{
static Bowl bowl1 = new Bowl(1); // public Table()
{
System.out.println("Table()"); //
bowl2.f1(1); //
} void f2(int marker)
{
System.out.println("f2(" + marker + ")");
} static Bowl bowl2 = new Bowl(2); //
} class Cupboard
{
Bowl bowl3 = new Bowl(3); //9 //14 // static Bowl bowl4 = new Bowl(4); // public Cupboard()
{
System.out.println("Cupboard()");//10 //15 //
bowl4.f1(2);//11 //16 //
} void f3(int marker)
{
System.out.println("f3(" + marker + ")");
} static Bowl bowl5 = new Bowl(5); //
} //output:
//Bowl(1)
//Bowl(2)
//Table()
//f1(1)
//Bowl(4)
//Bowl(5)
//Bowl(3)
//Cupboard()
//f1(2)
//Creating new Cupboard() in main
//Bowl(3)
//Cupboard()
//f1(2)
//Creating new Cupboard() in main
//Bowl(3)
//Cupboard()
//f1(2)
//f2(1)
//f3(1)
public class StaticIntialization
{
public static void main(String[] args)
{
System.out.println("Creating new Cupboard() in main");//
new Cupboard();//
System.out.println("Creating new Cupboard() in main");
new Cupboard(); //
table.f2(1); //
cupboard.f3(1);//
} static Table table = new Table(); // static Cupboard cupboard = new Cupboard(); //
}
3、类的创建过程是从基类向外扩散的,所以基类在导出类构造器可以访问它之前,就已经完成了初始化。
package com.ietree.base.reuseclass.extendskey; class Art
{
Art()
{
System.out.println("Art constructor"); //
}
} class Drawing extends Art
{
Drawing()
{
System.out.println("Drawing constructor"); //
}
} // output:
// Art constructor
// Drawing constructor
// Cartoon constructor
public class Cartoon extends Drawing
{
public Cartoon()
{
System.out.println("Cartoon constructor"); //
} public static void main(String[] args)
{
@SuppressWarnings("unused")
Cartoon x = new Cartoon(); //
}
}
4、如果Java的基类拥有某个已被多次重载的方法名称,那么在子类中重新定义该方法名称并不会屏蔽其在基类中的任何版本
package com.ietree.base.reuseclass.hide; class Homer
{
char doh(char c)
{
System.out.println("doh(char)");
return 'd';
} float doh(float f)
{
System.out.println("doh(float)");
return 1.0f;
}
} class Milhouse
{
} class Bart extends Homer
{
void doh(Milhouse m)
{
System.out.println("doh(Milhouse)");
}
} public class Hide
{
public static void main(String[] args)
{
Bart b = new Bart();
b.doh(1);
b.doh('x');
b.doh(new Milhouse());
}
}
// output:
// doh(float)
// doh(char)
// doh(Milhouse)
重读《Java编程思想》的更多相关文章
- 重读《Struts In Action》
Figure 1.1. The Java Servlet API exposes the HTTP client/server protocol to the Java platform. S ...
- 关于Spring的Controller及Struts的Action的多线程的注意
struts是线程安全,并不是指多线程,而是指单态,当多个用户访问一个请求的时候,服务器内存中只有一个与之对应的action类对象,execute方法加上了同步关键字,如果你在action里加上一个全 ...
- struts中action名称反复导致的神秘事件
近期由于项目需求变更.须要本人对当中的某个业务功能进行改动.本人依照前台页面找action,依据action找代码的逻辑进行了改动(公司项目是ssh框架,struts配置全部是通过注解的方式进行.配置 ...
- 实现Spring管理struts的Action
struts2和spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean. 可以在struts2中配置: <struts> ...
- (五)Struts之Action类基础(二)
上一章节末((三)Struts之Action类基础(一))介绍了如何获取用户输入数据的获取.接着就是在Struts中怎么把数据响应给用户端,这就必须要求我们把数据放到作用域中,然后才能显示到用户浏览器 ...
- JavaWeb_(Struts2框架)Struts创建Action的三种方式
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- Struts中Action三种接收参数的方式?
前言: 前面已经有一篇随笔介绍了Struts2的大概原理.本文就Struts2中Action与jsp页面进行数据对接时介绍几种常见方法! 值栈ValueStack 3个Action Action1 p ...
- struts+service+action+数据库
用户登录流程 1.jsp根据form表单中的action的login <form action="/test02/login" method="post&quo ...
- 初次了解struts的action类
Action类真正实现应用程序的事务逻辑,它们负责处理请求.在收到请求后,ActionServlet会为这个请求选择适当的Action 如果需要,创建Action的一个实例 调用Action的perf ...
- struts 在Action中访问web元素(request,session等)
出发jsp: <?xml version="1.0" encoding="GB18030" ?> <%@ page language=&quo ...
随机推荐
- delphi 10 Seattle 第一个Android程序
delphi 10 Seattle 第一个Android程序 1.打开Delphi RAD Studio Seattle,如下图 2.选择black application 点击OK 3. ...
- vue Element-UI组件
一.UI组件 目的: 提高开发效率, 别人提供好一切, 拿过来直接用饿了么团队开源一个基于vue组件库 Element-UI ==> pc端 文档: http://element-cn.elem ...
- iOS 8 AutoLayout与Size Class自悟
前言 iOS8和iPhone6发布已经过去蛮久了,广大的果粉终于迎来了大屏iPhone,再也不用纠结为大屏买三星舍苹果了…但是对于iOS开发人员来说,迎来了和Android开发开发一样的问题—> ...
- Spring加载xsd文件报错:because 1) could not find the document; 2) the document could not be read...
Spring启动时加载xml文件的过程: spring在加载xsd文件时总是先试图在本地查找xsd文件(spring的jar包中已经包含了所有版本的xsd文件),如果没有找到,才会转向去URL指定的路 ...
- 解决IE6下透明图片有背景的问题
描述,透底的图片,在其他浏览器中效果: 在IE6中的效果: 1.改图片 IMG 换用gif格式的图片,但是这样的话图片效果不好: 想让IE6用gif的图片,其他浏览器用png图片,但是只有IE有这样 ...
- MYSQL中GROUP BY不包含所有的非聚合字段时的注意事项
本文导读:在MYSQL中使用GROUP BY分组时,我们可以select 多个非聚合字段,但是这些字段不在GROUP BY中,这样的SQL查询在SQL SERVER.ORACLE中是不合理的,且会报错 ...
- easyui_1
--- easyui.css包括所有组件的css,
- poj1001 Exponentiation【java大数】
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 183034 Accepted: 44062 ...
- hdu1166 敌兵布阵【线段树】
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- Cloudrea manager5安装CDH5文档
一.主机规划.存储规划 服务器配置信息:CentOS6.5 最小化安装+development tools组包,其余组件yum安装即可. 二.系统设置如下: 1.服务器信息如下(/etc/hosts文 ...