(三)《Java编程思想》——构造函数初始化
1.初始化顺序是由变量在类内的定义顺序决定的,并且先初始化变量,然后才调用构造函数。
package chapter4;//: OrderOfInitialization.java
/**
* 初始化顺序
*/
class Tag {
Tag(int marker) {
System.out.println("Tag(" + marker + ")");
}
}
class Card {
Tag t1 = new Tag(1); // Before constructor
Card() {
// Indicate we're in the constructor:
System.out.println("Card()");
t3 = new Tag(33); // Re-initialize t3
}
Tag t2 = new Tag(2); // After constructor
void f() {
System.out.println("f()");
}
Tag t3 = new Tag(3); // At end
}
public class OrderOfInitialization {
public static void main(String[] args) {
Card t = new Card();
t.f(); // Shows that construction is done
}
} // /:~
【运行结果】:
Tag(1)
Tag(2)
Tag(3)
Card()
Tag(33)
f()
2. 静态数据的初始化
先来看看main函数所在类。main是程序的入口,调用一切对象必须先实例化。
package chapter4;
public class StaticInitialization {public static void main(String[] args) {
StaticInitialization s = new StaticInitialization();
System.out.println("i="+s.i);
}int i;
}
【运行结果】:i=0
如果变量为静态,将在main函数之前初始化。
package chapter4;
public class StaticInitialization1 {public static void main(String[] args) {
System.out.println("i="+i);
}
static int i=5;
}
【运行结果】:i=5
实例化类将默认调用构造函数
package chapter4;
class Table {Table() {
System.out.println("Table()");
}
}
public class StaticInitialization1 {
public static void main(String[] args) {
Table t2 = new Table();
}
}
【运行结果】:Table()
如果变量为静态,将在main函数之前初始化。同样适用于类(类是一种自定义变量)
package chapter4;
class Table {Table() {
System.out.println("Table()");
}
}
public class StaticInitialization1 {
public static void main(String[] args) {
}
static Table t2 = new Table();
}
【运行结果】:Table()
类内变量、静态变量初始化及构造函数调用顺序:
静态变量、变量、构造函数
package chapter4;class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
}
class Table {
Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
}
static Bowl b2 = new Bowl(2);
}
public class StaticInitialization1 {
public static void main(String[] args) {
}
static Table t2 = new Table();
}
【运行结果】:
Bowl(2)
Bowl(1)
Table()
static 初始化仅发生一次
package chapter4;class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Table {
Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
static Bowl b2 = new Bowl(2);
}
public class StaticInitialization1 {
public static void main(String[] args) {
System.out.println("=========main===========");
Table t3 = new Table();
t2.f2(2);
}
static Table t2 = new Table();
}
【运行结果】:
Bowl(2)
Bowl(1)
Table()
=========main===========
Bowl(1)
Table()
f2(2)
静态块:仅执行一次——首次生成那个类的一个对象时,或者首次访问属于那个类的一个static 成员时
//: ExplicitStatic.java// Explicit static initialization
// with the "static" clause.
class Cup {
Cup(int marker) {
System.out.println("Cup(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Cups {
static Cup c1;
static Cup c2;
static {
c1 = new Cup(1);
c2 = new Cup(2);
}
Cups() {
System.out.println("Cups()");
115
}
}
public class ExplicitStatic {
public static void main(String[] args) {
System.out.println("Inside main()");
Cups.c1.f(99); // (1)
}
static Cups x = new Cups(); // (2)
static Cups y = new Cups(); // (2)
} ///:~
【运行结果】:
Cup(1)
Cup(2)
Cups()
Cups()
Inside main()
f(99)
非静态实例的初始化可定义成与静态块类似的形式:
package chapter4;//: Mugs.java
// Java 1.1 "Instance Initialization"
class Mug {
Mug(int marker) {
System.out.println("Mug(" + marker + ")");
}
}
public class Mugs {
Mug c1;
Mug c2;
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}
Mugs() {
System.out.println("Mugs()");
}
public static void main(String[] args) {
System.out.println("Inside main()");
Mugs x = new Mugs();
}
} // /:~
【运行结果】:
Inside main()
Mug(1)
Mug(2)
c1 & c2 initialized
Mugs()
这段定义代码
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}
看起来与静态初始化从句极其相似,只是static 关键字从里面消失了。为支持对“匿名内部类”的初始化必须采用这一语法格式。
以上是 (三)《Java编程思想》——构造函数初始化 的全部内容, 来源链接: utcz.com/z/390805.html