相关最新代码已上传至我的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编程思想》的更多相关文章

  1. 重读《Struts In Action》

    Figure   1.1. The Java Servlet API exposes the HTTP client/server protocol to the Java   platform. S ...

  2. 关于Spring的Controller及Struts的Action的多线程的注意

    struts是线程安全,并不是指多线程,而是指单态,当多个用户访问一个请求的时候,服务器内存中只有一个与之对应的action类对象,execute方法加上了同步关键字,如果你在action里加上一个全 ...

  3. struts中action名称反复导致的神秘事件

    近期由于项目需求变更.须要本人对当中的某个业务功能进行改动.本人依照前台页面找action,依据action找代码的逻辑进行了改动(公司项目是ssh框架,struts配置全部是通过注解的方式进行.配置 ...

  4. 实现Spring管理struts的Action

    struts2和spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean.  可以在struts2中配置:  <struts>      ...

  5. (五)Struts之Action类基础(二)

    上一章节末((三)Struts之Action类基础(一))介绍了如何获取用户输入数据的获取.接着就是在Struts中怎么把数据响应给用户端,这就必须要求我们把数据放到作用域中,然后才能显示到用户浏览器 ...

  6. JavaWeb_(Struts2框架)Struts创建Action的三种方式

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  7. Struts中Action三种接收参数的方式?

    前言: 前面已经有一篇随笔介绍了Struts2的大概原理.本文就Struts2中Action与jsp页面进行数据对接时介绍几种常见方法! 值栈ValueStack 3个Action Action1 p ...

  8. struts+service+action+数据库

    用户登录流程 1.jsp根据form表单中的action的login   <form action="/test02/login" method="post&quo ...

  9. 初次了解struts的action类

    Action类真正实现应用程序的事务逻辑,它们负责处理请求.在收到请求后,ActionServlet会为这个请求选择适当的Action 如果需要,创建Action的一个实例 调用Action的perf ...

  10. struts 在Action中访问web元素(request,session等)

    出发jsp: <?xml version="1.0" encoding="GB18030" ?> <%@ page language=&quo ...

随机推荐

  1. c++11 delete禁用函数

    c++11添加了delete关键字. 不想用那个函数,在那个函数后面加 = delete就可以了: 比如: 在函数重载中,可用 delete 来滤掉一些函数的形参类型,如下: bool IsLucky ...

  2. Qt编写气体安全管理系统(界面超漂亮)

    自从把Qt样式表葵花宝典这个pdf文件看完以后,将所有的qss内容都轮了一遍,还写了个皮肤生成器工具,https://blog.csdn.net/feiyangqingyun/article/deta ...

  3. 题目1002:Grading(题目背景基于高考打分的简单判断)

    题目链接:http://ac.jobdu.com/problem.php?pid=1002 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  4. 简易扩展Visual Studio UnitTesting支持TestMethodCase

    NUnit的TestCaseAttribute可以简化大量的测试参数输入用例的编写,如果基于Visual Studio Unit Test Project开发则默认没有类似的功能,看一段对比代码: p ...

  5. 一、laya学习笔记 --- layabox环境搭建 HelloWorld(坑:ts版本问题解决方案)

    好吧,使用layabox需要从官网下载些啥呢 一.下载layabox 官网 https://www.layabox.com/ 首页上有两个,一个Engine,一个IDE Engine我下载的TS版本, ...

  6. 23种设计模式之适配器模式(Adapter)

    适配器模式将一个接口转换成客户希望的另一个接口,从而使接口不兼容的那些类可以一起工作.适配器模式既可以作为类结构型模式,也可以作为对象结构型模式.在类适配器模式中,通过使用一个具体类将适配者适配到目标 ...

  7. IOS控制系统手势返回

    self.navigationController.interactivePopGestureRecognizer.enabled = YES; //手势返回的代理,如果自定义了leftButtonI ...

  8. 在线工具-程序员的工具箱-在线Cron表达式生成器

    在线Cron表达式生成器 http://cron.qqe2.com/ 在线工具 - 程序员的工具箱 https://tool.lu/

  9. RabbitMQ服务端配置详解(转自:http://www.cnblogs.com/zhen-rh/p/6884297.html)

    RabbitMQ支持三种配置方式: 1) 读取环境变量中配置, 这包括shell中环境变量和rabbitmq-env.conf/rabbitmq-env-conf.bat文件中配置的环境变量 可配置如 ...

  10. nginx配置虚拟主机之不同端口和不同IP地址

    配置nginx虚拟主机不同端口和不同ip地址,和上编nginx基于域名配置虚拟主机博文类似,请先参考. zxl.com域名不同端口,配置文件内容如下: 1 2 3 4 5 6 7 8 9 10 11 ...