日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java初始化

發布時間:2025/5/22 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java初始化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java中一切皆對象,那就從對象初始化開始說起。

?示例代碼:

1 package test; 2 3 public class A extends B { 4 5 int i; 6 static int b; 7 public D firstD = new D("A"); 8 public static C firstC = new C("A"); 9 public static C secondC; 10 { 11 System.out.println("i:"+i);//此時i未初始化,為默認值0 12 System.out.println("b:"+b); 13 System.out.println(secondC); 14 } 15 A(){ 16 17 System.out.println("here is A"); 18 i = 1; 19 b = 1; 20 } 21 22 public static void main(String[] args) { 23 // TODO 自動生成的方法存根 24 new A(); 25 } 26 27 } 28 29 class B { 30 public static C CinB = new C("B"); 31 public D DinB = new D("B"); 32 B(){ 33 System.out.println("here is B"); 34 } 35 } 36 37 class C { 38 C(String source){ 39 System.out.println("here is C from"+source); 40 } 41 } 42 43 class D { 44 D(String source){ 45 System.out.println("here is D from"+source); 46 } 47 }

?

輸出:

here is C fromB
here is C fromA
here is D fromB
here is B
here is D fromA
i:0
b:0
null
here is A

?

結論:

1.在初始化時,首先為對象分配一塊存儲空間(默認為二進制0)

2.初始化順序:基類靜態成員->子類靜態成員->基類非靜態成員/實例初始化->基類構造器->子類非靜態成員/實例初始化->子類構造器

轉載于:https://www.cnblogs.com/xiaowk/p/10465818.html

總結

以上是生活随笔為你收集整理的java初始化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。