重读《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 ...
随机推荐
- String例子
#include <string.h> class String{ public: String(const String& str); String(const char* st ...
- 【linux】nginx options 跨域问题 请求HTTP错误405 用于访问该页的HTTP动作未被许可 Method Not Allowed
JavaScript JS 跨域问题 HTTP 错误 405 - 用于访问该页的 HTTP 动作未被许可HTTP 错误 405.0 - Method Not Allowed Nginx 处理跨域问题. ...
- 【Web前端开发最佳实践系列】标准的HTML代码
一.验证代码是否符合标准 优点: 标准的页面会保证浏览器正确的渲染 网页能更容易被搜索引擎搜索,提高网站的搜索排名 提高网站的易用性 网页更好维护和扩展 常用工具: W3 Validator HTML ...
- C# EF中调用 存储过程并调回参数
TourEntities db = new TourEntities(); List<v_product> v = new List<v_product>(); SqlPara ...
- window下node更新
打开cmd查看你之前node版本安装的路径,where node: 直接去官网下载与你电脑系统(32位还是64位)对应的最新的mis版本,安装在上述路径中覆盖即可. 注意:windows上并不支持n模 ...
- 让google.com不跳转到google.com.hk
自从google的服务器搬离中国大陆后,大陆地区用户用google服务时会自动跳转到香港的http://google.com.hk,,有关键字过滤而且偶尔不是很稳定,这对我们的生活工作都造成了困扰. ...
- shell 中的$0 $1 $* $@ $# $$ $? $() $(())
$0: 脚本本身文件名称 : 命令行第一个参数,$2为第二个,以此类推 $*: 所有参数列表 $@: 所有参数列表 $#: 参数个数 $$: 脚本运行时的PID $?: 脚本退出码 ∗与@的区别 当命 ...
- jQuery 核心 - noConflict() 方法
1.遇到问题: 当我们写jquery时使用$,发现写的jquery全部失效: 2.发现问题: 排查后发现是noConflict()函数在作怪,因为使用noConflict()函数后,重新定义$名字为j ...
- mysql操作常用技巧
删除一张表的数据,条件在另一张表 delete a from A a,B b where a.tel=b.tel and a.code=b.code and a.day='201808';
- yii---where该如何使用
简单示例yii 的where使用方法: $where = ['post_id'=>$postId]; //$list = ForumThreadPost::find()->where($w ...