thinking in Java 笔记 5

Non-static instance initialization
Java provides a similar syntax, called instance initialization, for initializing non-static
variables for each object. Here’s an example:
//: initialization/Mugs.java
// Java "Instance Initialization."
import static net.mindview.util.Print.*;
class Mug {
Mug(int marker) {
print("Mug(" + marker + ")");
}
void f(int marker) {
print("f(" + marker + ")");
}
}
public class Mugs {
Mug mug1;
Mug mug2;
{
mug1 = new Mug(1);
mug2 = new Mug(2);
print("mug1 & mug2 initialized");
}
Mugs() {
print("Mugs()");
}
Mugs(int i) {
print("Mugs(int)");
}
public static void main(String[] args) {
print("Inside main()");
new Mugs();
print("new Mugs() completed");
new Mugs(1);
print("new Mugs(1) completed");
}
} /* Output:
Inside main()
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs()
new Mugs() completed
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs(int)
new Mugs(1) completed
*///:~
You can see that the instance initialization clause:
{
mug1 = new Mug(1);
mug2 = new Mug(2);
print("mug1 & mug2 initialized");
}
looks exactly like the static initialization clause except for the missing static keyword. This
syntax is necessary to support the initialization of anonymous inner classes (see the Inner
Classes chapter), but it also allows you to guarantee that certain operations occur regardless
of which explicit constructor is called. From the output, you can see that the instance

initialization clause is executed before either one of the constructors.


p.166

Composition syntax
Composition has been used quite frequently up to this point in the book. You simply place
object references inside new classes. For example, suppose you’d like an object that holds
several String objects, a couple of primitives, and an object of another class. For the non-
primitive objects, you put references inside your new class, but you define the primitives
directly:
//: reusing/SprinklerSystem.java
// Composition for code reuse.
class WaterSource {
private String s;
WaterSource() {
System.out.println("WaterSource()");
s = "Constructed";
}
public String toString() { return s; }

public class SprinklerSystem {
private String valve1, valve2, valve3, valve4;
private WaterSource source = new WaterSource();
private int i;
private float f;
public String toString() {
return
"valve1 = " + valve1 + " " +
"valve2 = " + valve2 + " " +
"valve3 = " + valve3 + " " +
"valve4 = " + valve4 + "\n" +
"i = " + i + " " + "f = " + f + " " +
"source = " + source;
}
public static void main(String[] args) {
SprinklerSystem sprinklers = new SprinklerSystem();
System.out.println(sprinklers);
}
} /* Output:
WaterSource()
valve1 = null valve2 = null valve3 = null valve4 = null
i = 0 f = 0.0 source = Constructed
*///:

One of the methods defined in both classes is special: toString( ). Every non-primitive
object has a toString( ) method, and it’s called in special situations when the compiler
wants a String but it has an object. So in the expression in SprinklerSystem.toString( ):
"source = " + source;
the compiler sees you trying to add a String object ("source = ") to a WaterSource.
Because you can only “add” a String to another String, it says “I’ll turn source into a
String by calling toString( )!” After doing this it can combine the two Strings and pass the
resulting String to System.out.println( ) (or equivalently, this book’s print() and
printnb( ) static methods). Any time you want to allow this behavior with a class you
create, you need only write a toString( ) method.
Primitives that are fields in a class are automatically initialized to zero, as noted in the
Everything Is an Object chapter. But the object references are initialized to null, and if you
try to call methods for any of them, you’ll get an exception-a runtime error. Conveniently,
you can still print a null reference without throwing an exception.
It makes sense that the compiler doesn’t just create a default object for every reference,
because that would incur unnecessary overhead in many cases. If you want the references
initialized, you can do it:
1.  At the point the objects are defined. This means that they’ll always be initialized
before the constructor is called.
2.  In the constructor for that class.
3.  Right before you actually need to use the object. This is often called lazy
initialization. It can reduce overhead in situations where object creation is expensive
and the object doesn’t need to be created every time.
4.  Using instance initialization.
All four approaches are shown here:
//: reusing/Bath.java
// Constructor initialization with composition.
import static net.mindview.util.Print.*;
class Soap {
private String s;
Soap() {
print("Soap()");
s = "Constructed";
}
public String toString() { return s; }

public class Bath {
private String // Initializing at point of definition:
s1 = "Happy",
s2 = "Happy",
s3, s4;
private Soap castille;
private int i;
private float toy;
public Bath() {
print("Inside Bath()");
s3 = "Joy";
toy = 3.14f;
castille = new Soap();
}
// Instance initialization:
{ i = 47; }
public String toString() {
if(s4 == null) // Delayed initialization:
s4 = "Joy";
return
"s1 = " + s1 + "\n" +
"s2 = " + s2 + "\n" +
"s3 = " + s3 + "\n" +
"s4 = " + s4 + "\n" +
"i = " + i + "\n" +
"toy = " + toy + "\n" +
"castille = " + castille;
}
public static void main(String[] args) {
Bath b = new Bath();
print(b);
}
} /* Output:
Inside Bath()
Soap()
s1 = Happy
s2 = Happy
s3 = Joy
s4 = Joy
i = 47
toy = 3.14
castille = Constructed
*///:~
Note that in the Bath constructor, a statement is executed before any of the initializations
take place. When you don’t initialize at the point of definition, there’s still no guarantee that
you’ll perform any initialization before you send a message to an object reference—except for
the inevitable run-time exception.
When toString( ) is called it fills in s4 so that all the fields are properly initialized by the
time they are used.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值