標籤:system data data- too ati .net 變數 rgs highlight
原始碼:
[java] view plain copy
- public class SuperAndSub {
-
- public static void main(String[] args) {
-
- // Super s1 = new Sub();
- // Super s2 = new Super();
- Sub s3 = new Sub();
- }
- }
-
- class Super {
-
- static int a = getA();
-
- static {
- System.out.println("載入Super的靜態塊");
- }
-
- int b = getB();
-
- {
- System.out.println("載入Super的普通塊");
- }
-
- Super() {
- System.out.println("載入Super的構造器");
- }
-
- static int getA() {
- System.out.println("載入Super的靜態變數");
- return 1;
- }
-
- static int getB() {
- System.out.println("載入Super的執行個體變數");
- return 2;
- }
-
- }
-
- class Sub extends Super {
-
- static int c = getC();
-
- static {
- System.out.println("載入Sub的靜態塊");
- }
-
- int d = getD();
-
- {
- System.out.println("載入Sub的普通塊");
- }
-
- Sub() {
- System.out.println("載入Sub的構造器");
- }
-
- static int getC() {
- System.out.println("載入Sub的靜態變數");
- return 1;
- }
-
- static int getD() {
- System.out.println("載入Sub的執行個體變數");
- return 2;
- }
- }
輸出:
[html] view plain copy
- 載入Super的靜態變數
- 載入Super的靜態塊
- 載入Sub的靜態變數
- 載入Sub的靜態塊
- 載入Super的執行個體變數
- 載入Super的普通塊
- 載入Super的構造器
- 載入Sub的執行個體變數
- 載入Sub的普通塊
- 載入Sub的構造器
Java類中的各種成員的載入順序