首页 > 试题广场 >

阅读以下程序,写出输出结果。

[问答题]

阅读以下程序,写出输出结果。

class LargeCup {
  LargeCup(int marker) {
  System.out.println("LargeCup(" + marker + ")");
  }
  void f1(int marker) {
  System.out.println("f1(" + marker + ")");
  }
}
 
class Shelf {
  static LargeCup cup1 = new LargeCup(1);
  Shelf() {
  System.out.println("Shelf()");
    cup2.f1(1);
  }
  void f2(int marker) {
  System.out.println("f2(" + marker + ")");
  }
  static LargeCup cup2 = new LargeCup(2);
}
 
class Cupshelf {
  static LargeCup cup3 = new LargeCup(3);
  static LargeCup cup4 = new LargeCup(4);
  Cupshelf() {
  System.out.println("Cupshelf()");
    cup4.f1(2);
  }
  void f3(int marker) {
  System.out.println("f3(" + marker + ")");
  }
  static LargeCup cup5 = new LargeCup(5);
}
 
public class Initialization {
  static Shelf shelf = new Shelf();
  static Cupshelf cupshelf = new Cupshelf();
  public static void main(String[] args) {
System.out.println("Creating new Cupshelf1() in main");
    new Cupshelf();
    System.out.println("Creating new Cupshelf2() in main");
    new Cupshelf();
    shelf.f2(1);
cupshelf.f3(1);
new Initialization();
  }
}

LargeCup(1)
LargeCup(2)
Shelf()
f1(1)
LargeCup(3)
LargeCup(4)
LargeCup(5)
Cupshelf()
f1(2)
Creating new Cupshelf1() in main
Cupshelf()
f1(2)
Creating new Cupshelf2() in main
Cupshelf()
f1(2)
f2(1)
f3(1)

发表于 2017-05-17 16:37:17 回复(0)
静态类/方法/代码块,会在类初始化的时候运行一次
发表于 2017-08-26 17:25:01 回复(0)